Python(데이터 수집과 그룹 연산)

짬그브·2025년 3월 5일

개요

데이터 집합을 분류하고 각 그룹별로 집계나 변형 같은 어떤 함수를 적용 하는 건 데이터 분석 과정에서 무척 중요한 일이다.

데이터를 불러오고 취합해서 하나의 데이터 집합을 준비하고 나면 그룹 통계를 구하거나 가능하면 피벗 테이블을 구해서 보고서를 만들거나 시각화하게 된다.

Pandas는 데이터 집합을 자연스럽게 나누고 요약할 수 있는 groupby라는 유연한 방법을 제공한다.

GroupBy 메카닉

분리 - 적용 - 결합

객체에 들어 있는 데이터를 하나 이상의 색인 기준으로 분리한다.

함수를 각 그룹에 적용시켜 새로운 값을 얻어 낸다.

함수를 적용한 결과를 하나의 객체로 결합한다.

from tokenize import group

import pandas as pd
import numpy as np


df = pd.DataFrame({'key1':['a','a','b','b','a'],
                   'key2':['one','two','one','two','one'],
                   'data1':np.random.randn(5),
                   'data2':np.random.randn(5)})
print(df)
print()

grouped1 = df.groupby('key1')
print(grouped1)
print()

for name,gd in grouped1:
    print(name)
    print(gd, end='\n\n')


print()
print(grouped1[['data1','data2']].mean())
print()
print(grouped1[['data2']].mean())
print()

print(list(grouped1))
ldata = list(grouped1)
print(ldata[1])
print(ldata[1][1])
print()

print(dict(ldata)['b'])
print()

grouped2 = df.groupby(['key1','key2'])
for gd in grouped2:
    print(gd, end='\n\n')
print()

print(grouped2[['data1','data2']].sum())






  key1 key2     data1     data2
0    a  one  1.930759  0.353781
1    a  two -0.511214  1.521039
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296
4    a  one -0.423424  1.144649

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x00000165C11CCFA0>

a
  key1 key2     data1     data2
0    a  one  1.930759  0.353781
1    a  two -0.511214  1.521039
4    a  one -0.423424  1.144649

b
  key1 key2     data1     data2
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296


         data1     data2
key1                    
a     0.332040  1.006489
b     0.581033  0.249035

         data2
key1          
a     1.006489
b     0.249035

[('a',   key1 key2     data1     data2
0    a  one  1.930759  0.353781
1    a  two -0.511214  1.521039
4    a  one -0.423424  1.144649), ('b',   key1 key2     data1     data2
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296)]
('b',   key1 key2     data1     data2
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296)
  key1 key2     data1     data2
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296

  key1 key2     data1     data2
2    b  one  0.550386  0.577365
3    b  two  0.611680 -0.079296

(('a', 'one'),   key1 key2     data1     data2
0    a  one  1.930759  0.353781
4    a  one -0.423424  1.144649)

(('a', 'two'),   key1 key2     data1     data2
1    a  two -0.511214  1.521039)

(('b', 'one'),   key1 key2     data1     data2
2    b  one  0.550386  0.577365)

(('b', 'two'),   key1 key2    data1     data2
3    b  two  0.61168 -0.079296)


              data1     data2
key1 key2                    
a    one   1.507335  1.498430
     two  -0.511214  1.521039
b    one   0.550386  0.577365
     two   0.611680 -0.079296

그룹 간 순회

GroupBy객체는 이터레이션을 지원하는데, 그룹 이름과 그에 따른 데이터 묶음을 튜플로 반환한다.

import numpy as np
import pandas as pd


np.random.seed(12345)

frame = pd.DataFrame(np.random.randn(5,5),
                     index=['joe','steve','wes','jim','travis'],
                     columns=list('abcde'))
print(frame)
print()

mapping = {'a':'red','b':'red','c':'blue','d':'blue','e':'red'}
grouped1= frame.T.groupby(mapping)
print()
print(grouped1.sum())
print()

labeling = ['one','one','one','two','two']
grouped2 = frame.groupby(labeling)
print(grouped2.mean())
print()

grouped3 = frame.groupby(len)
print(grouped3.sum())

grouped3 = frame.groupby(lambda x: x[-1])
print(grouped3.sum())
print()

print(frame.groupby([len, labeling]).min())


               a         b         c         d         e
joe    -0.204708  0.478943 -0.519439 -0.555730  1.965781
steve   1.393406  0.092908  0.281746  0.769023  1.246435
wes     1.007189 -1.296221  0.274992  0.228913  1.352917
jim     0.886429 -2.001637 -0.371843  1.669025 -0.438570
travis -0.539741  0.476985  3.248944 -1.021228 -0.577087


           joe     steve       wes       jim    travis
blue -1.075169  1.050769  0.503905  1.297183  2.227716
red   2.240016  2.732748  1.063885 -1.553778 -0.639844

            a         b         c         d         e
one  0.731963 -0.241457  0.012433  0.147402  1.521711
two  0.173344 -0.762326  1.438551  0.323899 -0.507829

          a         b         c         d         e
3  1.688911 -2.818915 -0.616290  1.342208  2.880128
5  1.393406  0.092908  0.281746  0.769023  1.246435
6 -0.539741  0.476985  3.248944 -1.021228 -0.577087
          a         b         c         d         e
e  1.188698  0.571851 -0.237693  0.213292  3.212215
m  0.886429 -2.001637 -0.371843  1.669025 -0.438570
s  0.467448 -0.819236  3.523936 -0.792315  0.775830

              a         b         c         d         e
3 one -0.204708 -1.296221 -0.519439 -0.555730  1.352917
  two  0.886429 -2.001637 -0.371843  1.669025 -0.438570
5 one  1.393406  0.092908  0.281746  0.769023  1.246435
6 two -0.539741  0.476985  3.248944 -1.021228 -0.577087

칼럼 선택

DataFrame 에서 만든 GroupBy 객체를 칼럼 이름이나 칼럼 이름이 담긴 배열로 색인하면 수집을 위해 해당 칼럼을 선택 하게 된다.

import numpy as np
import pandas as pd

tips = pd.read_csv('tips.csv')
print(tips.head(10))
tips.info()
print()

tips['tip_pct'] = tips['tip'] / tips['total_bill']
print(tips.head())
print()

grouped = tips.groupby(['sex','smoker'])
grouped_pct = grouped['tip_pct']
print(grouped_pct.mean())
print()
print(grouped_pct.agg('mean'))
print()
print(grouped_pct.agg(['mean','std']))  # function 여러가지 이용가능

print()
print(grouped_pct.agg([('평균값','mean'),'std'])) # 튜플을 사용하여 이름을 바꿀 수 있음

print(grouped.agg({'tip':'max','size':'sum'}))
print()

print(grouped.agg({'tip_pct':['min','max'],
                  'size':['sum','mean','std']}))

   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4
5       25.29  4.71    Male     No  Sun  Dinner     4
6        8.77  2.00    Male     No  Sun  Dinner     2
7       26.88  3.12    Male     No  Sun  Dinner     4
8       15.04  1.96    Male     No  Sun  Dinner     2
9       14.78  3.23    Male     No  Sun  Dinner     2
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   total_bill  244 non-null    float64
 1   tip         244 non-null    float64
 2   sex         244 non-null    object 
 3   smoker      244 non-null    object 
 4   day         244 non-null    object 
 5   time        244 non-null    object 
 6   size        244 non-null    int64  
dtypes: float64(2), int64(1), object(4)
memory usage: 13.5+ KB

   total_bill   tip     sex smoker  day    time  size   tip_pct
0       16.99  1.01  Female     No  Sun  Dinner     2  0.059447
1       10.34  1.66    Male     No  Sun  Dinner     3  0.160542
2       21.01  3.50    Male     No  Sun  Dinner     3  0.166587
3       23.68  3.31    Male     No  Sun  Dinner     2  0.139780
4       24.59  3.61  Female     No  Sun  Dinner     4  0.146808

sex     smoker
Female  No        0.156921
        Yes       0.182150
Male    No        0.160669
        Yes       0.152771
Name: tip_pct, dtype: float64

sex     smoker
Female  No        0.156921
        Yes       0.182150
Male    No        0.160669
        Yes       0.152771
Name: tip_pct, dtype: float64

                   mean       std
sex    smoker                    
Female No      0.156921  0.036421
       Yes     0.182150  0.071595
Male   No      0.160669  0.041849
       Yes     0.152771  0.090588

                    평균값       std
sex    smoker                    
Female No      0.156921  0.036421
       Yes     0.182150  0.071595
Male   No      0.160669  0.041849
       Yes     0.152771  0.090588
              
                tip  size
sex    smoker            
Female No       5.2   140
       Yes      6.5    74
Male   No       9.0   263
       Yes     10.0   150
       
                tip_pct           size                    
                    min       max  sum      mean       std
sex    smoker                                             
Female No      0.056797  0.252672  140  2.592593  1.073146
       Yes     0.056433  0.416667   74  2.242424  0.613917
Male   No      0.071804  0.291990  263  2.711340  0.989094
       Yes     0.035638  0.710345  150  2.500000  0.892530


데이터 묶기

함수로 묶기

pandas는 파이썬 함수를 사용해서 그룹을 매핑할 수 있는 기능을 제공한다.

색인 단계로 묶기

계층적으로 색인된 데이터 묶음은 축 색인의 단계 중 하나를 사용해서 편리하게 모을 수 있는 기능을 제공한다.

데이터 수집

Pandas는 데이터 수집(데이터 묶음에 대한 준비된 통계)을 위한 최적화 된 메서드를 제공한다.

count : 그룹 내에 NA 값이 아닌 값의 수를 반환한다.
sum : NA 값이 아닌 값들의 합을 구한다.
mean : NA 값이 아닌 값들의 평균 값을 구한다.
median : NA 값이 아닌 값들의 산술 중간 값을 구한다.
std,var : 편향되지 않은(n-1을 분모로 하는) 표준편차와 분산
min,max : NA 값이 아닌 값 중 최소 값과 최대 값
prod : NA 값이 아닌 값의 곱
first,last : NA 값이 아닌 값들 중 첫 번째 값과 마지막 값

칼럼에 여러 가지 함수 적용하기

그룹별 연산과 변형

그룹 연산

pandas.apply() : 분리 - 적용 - 병합

import numpy as np
import pandas as pd

tips = pd.read_csv('tips.csv')
tips['tip_pct'] = tips['tip'] / tips['total_bill']

def g_func(gdata, n=5, column='tip_pct'):
    return gdata.sort_values(by=column, ascending=False)[:n]

print(tips.groupby('smoker').apply(g_func))
print()
print(tips.groupby(['smoker','day']).apply(g_func, n=3, column='total_bill'))

            total_bill   tip     sex smoker   day    time  size   tip_pct
smoker                                                                   
No     232       11.61  3.39    Male     No   Sat  Dinner     2  0.291990
       149        7.51  2.00    Male     No  Thur   Lunch     2  0.266312
       51        10.29  2.60  Female     No   Sun  Dinner     2  0.252672
       185       20.69  5.00    Male     No   Sun  Dinner     5  0.241663
       88        24.71  5.85    Male     No  Thur   Lunch     2  0.236746
Yes    172        7.25  5.15    Male    Yes   Sun  Dinner     2  0.710345
       178        9.60  4.00  Female    Yes   Sun  Dinner     2  0.416667
       67         3.07  1.00  Female    Yes   Sat  Dinner     1  0.325733
       183       23.17  6.50    Male    Yes   Sun  Dinner     4  0.280535
       109       14.31  4.00  Female    Yes   Sat  Dinner     2  0.279525
       
                 total_bill    tip     sex smoker   day    time  size   tip_pct
smoker day                                                                     
No     Fri  94        22.75   3.25  Female     No   Fri  Dinner     2  0.142857
            91        22.49   3.50    Male     No   Fri  Dinner     2  0.155625
            223       15.98   3.00  Female     No   Fri   Lunch     3  0.187735
       Sat  212       48.33   9.00    Male     No   Sat  Dinner     4  0.186220
            59        48.27   6.73    Male     No   Sat  Dinner     4  0.139424
            23        39.42   7.58    Male     No   Sat  Dinner     4  0.192288
       Sun  156       48.17   5.00    Male     No   Sun  Dinner     6  0.103799
            112       38.07   4.00    Male     No   Sun  Dinner     3  0.105070
            11        35.26   5.00  Female     No   Sun  Dinner     4  0.141804
       Thur 142       41.19   5.00    Male     No  Thur   Lunch     5  0.121389
            85        34.83   5.17  Female     No  Thur   Lunch     4  0.148435
            141       34.30   6.70    Male     No  Thur   Lunch     6  0.195335
Yes    Fri  95        40.17   4.73    Male    Yes   Fri  Dinner     4  0.117750
            90        28.97   3.00    Male    Yes   Fri  Dinner     2  0.103555
            96        27.28   4.00    Male    Yes   Fri  Dinner     2  0.146628
       Sat  170       50.81  10.00    Male    Yes   Sat  Dinner     3  0.196812
            102       44.30   2.50  Female    Yes   Sat  Dinner     3  0.056433
            207       38.73   3.00    Male    Yes   Sat  Dinner     4  0.077459
       Sun  182       45.35   3.50    Male    Yes   Sun  Dinner     3  0.077178
            184       40.55   3.00    Male    Yes   Sun  Dinner     2  0.073983
            180       34.65   3.68    Male    Yes   Sun  Dinner     4  0.106205
       Thur 197       43.11   5.00  Female    Yes  Thur   Lunch     4  0.115982
            83        32.68   5.00    Male    Yes  Thur   Lunch     2  0.152999
            192       28.44   2.56    Male    Yes  Thur   Lunch     2  0.090014

그룹에 국한된 값으로 누락된 값 채우기


import pandas as pd
import numpy as np

states = ['ohio','new york','vermont','florida',
          'oregon','nevada','california','idaho']

group_key = ['East'] * 4 + ['West'] * 4

data = pd.Series(np.random.randn(8),index=states)
#  print(data)
data[['vermont','nevada','idaho']] = np.nan
print(data)

print(data.groupby(group_key).mean())
print()

fill_mean = lambda g : g.fillna(g.mean())
print(data.groupby(group_key).apply(fill_mean))
print()

fill_values = {'East':0.5,'West':-1}
fill_func = lambda  g : g.fillna(fill_values[g.name])
print(data.groupby(group_key).apply(fill_func))

ohio          0.449226
new york     -0.584617
vermont            NaN
florida      -0.823783
oregon       -0.718627
nevada             NaN
california    0.526202
idaho              NaN
dtype: float64
East   -0.319725
West   -0.096212
dtype: float64

East  ohio          0.449226
      new york     -0.584617
      vermont      -0.319725
      florida      -0.823783
West  oregon       -0.718627
      nevada       -0.096212
      california    0.526202
      idaho        -0.096212
dtype: float64

East  ohio          0.449226
      new york     -0.584617
      vermont       0.500000
      florida      -0.823783
West  oregon       -0.718627
      nevada       -1.000000
      california    0.526202
      idaho        -1.000000
dtype: float64

랜덤 표본과 순열

가중 평균과 상관 관계

피벗 테이블과 교차일람표

피벗 테이블은 스프레드시트 프로그램과 다른 데이터 분석 소프트웨어에서 흔히 볼 수 있는 데이터 요약화 도구이다.

피벗 테이블은 하나 이상의 키로 수집해서 로우, 칼럼에 나열해서 데이터를 정렬한다,.

DataFrame.pivot_table()

values : 집계하려는 칼럼 이름 혹은 이름의 리스트, 기본 값으로 모든 숫자 칼럼을 집계한다.
rows : 만들어지는 피벗 테이블의 로우의 그룹으로 묶을 칼럼이나 그룹 키
cols : 만들어지는 피벗 테이블의 칼럼의 그룹으로 묶을 칼럼이나 그룹 키
aggfunc : 집계 함수나 함수 리스트, 기본 값은 'mean'이고 groupby 안에서 유효한 어떤 함수라도 가능하다.
fill_value : 누락된 값을 대체하기 위한 값
margins : 부분합이나 총계를 담기 위한 로우/칼럼을 추가할지의 여부

교차일람표는 그룹 빈도를 게산하기 위한 피벗 테이블의 특수한 경우이다.

import pandas as pd

tips = pd.read_csv('tips.csv')
tips['tip_pct'] = tips['tip'] / tips['total_bill']
print(tips.pivot_table(['tip_pct','size'],
                       index=['sex','smoker'],
                       aggfunc='mean'))
print()
print(tips.pivot_table(['tip_pct','size'],
                       index=['sex','smoker'],
                       aggfunc=['mean','std']))
print()

print(tips.pivot_table(['tip_pct','size'],
                       columns=['sex'],
                       index='day',
                       aggfunc='mean'))
print()

                   size   tip_pct
sex    smoker                    
Female No      2.592593  0.156921
       Yes     2.242424  0.182150
Male   No      2.711340  0.160669
       Yes     2.500000  0.152771

                   mean                 std          
                   size   tip_pct      size   tip_pct
sex    smoker                                        
Female No      2.592593  0.156921  1.073146  0.036421
       Yes     2.242424  0.182150  0.613917  0.071595
Male   No      2.711340  0.160669  0.989094  0.041849
       Yes     2.500000  0.152771  0.892530  0.090588

          size             tip_pct          
sex     Female      Male    Female      Male
day                                         
Fri   2.111111  2.100000  0.199388  0.143385
Sat   2.250000  2.644068  0.156470  0.151577
Sun   2.944444  2.810345  0.181569  0.162344
Thur  2.468750  2.433333  0.157525  0.165276

문제


import pandas as pd
import numpy as np

fec = pd.read_csv('P00000001-ALL.csv')
fec.info()
print()
print(fec.iloc[7])
print()

unique_cands = fec.cand_nm.unique()
print(unique_cands)
print()

parties = {'Bachmann, Michelle':'Republican',
           'Cain, Herman':'Republican',
           'Gingrich, Newt':'Republican',
           'Huntsman, Jon':'Republican',
           'Johnson, Gary Earl':'Republican',
           'McCotter, Thaddeus G':'Republican',
           'Obama, Barack':'Democrat',
           'Paul, Ron':'Republican',
           'Pawlenty, Timothy':'Republican',
           'Perry, Rick':'Republican',
           "Roemer, Charles E. 'Buddy' III":'Republican',
           'Romney, Mitt':'Republican',
           'Santorum, Rick':'Republican'}

occ_mapping = {
    'INFORMATION REQUESTED PER BEST EFFORTS':'NOT PROVIDED',
    'INFORMATION REQUESTED':'NOT PROVIDED',
    'INFORMATION REQUESTED (BEST EFFORTS)':'NOT PROVIDED',
    'C.E.O':'CEO',
    'C.E.O.':'CEO'
}


fec['party']= fec.cand_nm.map(parties)
print(fec.head())
print()

print(fec['party'].value_counts())
print()

fec = fec[fec.contb_receipt_amt > 0]
fec.info()

print()
print(fec.contbr_occupation.value_counts()[:10])
print()

cf = lambda x : occ_mapping.get(x, x)
fec.contbr_occupation = fec.contbr_occupation.map(cf)
print(fec.contbr_occupation.value_counts()[:10])
print()


by_occupation = fec.pivot_table('contb_receipt_amt',
                            index='contbr_occupation',
                            columns='party',
                            aggfunc='sum')

print(by_occupation)
print()

over_2mm = by_occupation[by_occupation.sum(axis=1)  > 2000000]
print(over_2mm)
print()

# import matplotlib.pyplot as plt
# over_2mm.plot(kind='barh')
# plt.show()

fec_mrbo = fec[fec.cand_nm.isin(['Obama, Barack','Romney, Mitt'])]
fec_mrbo.info()

def get_top_amounts(group, key, n=5):
    totals = group.groupby(key)['contb_receipt_amt'].sum()
    return totals.sort_values(ascending=False)[:n]

grouped = fec_mrbo.groupby('cand_nm')
print(grouped.apply(get_top_amounts, 'contbr_occupation', n=7, include_groups=False))
print()

bins = np.array([0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000])
labels = pd.cut(fec_mrbo.contb_receipt_amt , bins)
print(labels)
print()

grouped = fec_mrbo.groupby(['cand_nm',labels],observed=False)
bucket_sums = grouped.contb_receipt_amt.sum().unstack(level=0)
print(bucket_sums)
print()

normed_sums = bucket_sums.div(bucket_sums.sum(axis=1),axis=0) * 100
print(normed_sums)


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1001731 entries, 0 to 1001730
Data columns (total 16 columns):
 #   Column             Non-Null Count    Dtype  
---  ------             --------------    -----  
 0   cmte_id            1001731 non-null  object 
 1   cand_id            1001731 non-null  object 
 2   cand_nm            1001731 non-null  object 
 3   contbr_nm          1001731 non-null  object 
 4   contbr_city        1001712 non-null  object 
 5   contbr_st          1001727 non-null  object 
 6   contbr_zip         1001620 non-null  object 
 7   contbr_employer    988002 non-null   object 
 8   contbr_occupation  993301 non-null   object 
 9   contb_receipt_amt  1001731 non-null  float64
 10  contb_receipt_dt   1001731 non-null  object 
 11  receipt_desc       14166 non-null    object 
 12  memo_cd            92482 non-null    object 
 13  memo_text          97770 non-null    object 
 14  form_tp            1001731 non-null  object 
 15  file_num           1001731 non-null  int64  
dtypes: float64(1), int64(1), object(14)
memory usage: 122.3+ MB

cmte_id                       C00410118
cand_id                       P20002978
cand_nm              Bachmann, Michelle
contbr_nm              BLEVINS, DARONDA
contbr_city                     PIGGOTT
contbr_st                            AR
contbr_zip                  724548253.0
contbr_employer                    NONE
contbr_occupation               RETIRED
contb_receipt_amt                 250.0
contb_receipt_dt              05-JUL-11
receipt_desc                        NaN
memo_cd                             NaN
memo_text                           NaN
form_tp                           SA17A
file_num                         749073
Name: 7, dtype: object

['Bachmann, Michelle' 'Romney, Mitt' 'Obama, Barack'
 "Roemer, Charles E. 'Buddy' III" 'Pawlenty, Timothy' 'Johnson, Gary Earl'
 'Paul, Ron' 'Santorum, Rick' 'Cain, Herman' 'Gingrich, Newt'
 'McCotter, Thaddeus G' 'Huntsman, Jon' 'Perry, Rick']

     cmte_id    cand_id             cand_nm  ... form_tp file_num       party
0  C00410118  P20002978  Bachmann, Michelle  ...   SA17A   736166  Republican
1  C00410118  P20002978  Bachmann, Michelle  ...   SA17A   736166  Republican
2  C00410118  P20002978  Bachmann, Michelle  ...   SA17A   749073  Republican
3  C00410118  P20002978  Bachmann, Michelle  ...   SA17A   749073  Republican
4  C00410118  P20002978  Bachmann, Michelle  ...   SA17A   736166  Republican

[5 rows x 17 columns]

party
Democrat      593746
Republican    407985
Name: count, dtype: int64

<class 'pandas.core.frame.DataFrame'>
Index: 991475 entries, 0 to 1001730
Data columns (total 17 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   cmte_id            991475 non-null  object 
 1   cand_id            991475 non-null  object 
 2   cand_nm            991475 non-null  object 
 3   contbr_nm          991475 non-null  object 
 4   contbr_city        991457 non-null  object 
 5   contbr_st          991471 non-null  object 
 6   contbr_zip         991373 non-null  object 
 7   contbr_employer    983833 non-null  object 
 8   contbr_occupation  989133 non-null  object 
 9   contb_receipt_amt  991475 non-null  float64
 10  contb_receipt_dt   991475 non-null  object 
 11  receipt_desc       5219 non-null    object 
 12  memo_cd            89461 non-null   object 
 13  memo_text          94274 non-null   object 
 14  form_tp            991475 non-null  object 
 15  file_num           991475 non-null  int64  
 16  party              991475 non-null  object 
dtypes: float64(1), int64(1), object(15)
memory usage: 136.2+ MB

contbr_occupation
RETIRED                                   233990
INFORMATION REQUESTED                      35107
ATTORNEY                                   34286
HOMEMAKER                                  29931
PHYSICIAN                                  23432
INFORMATION REQUESTED PER BEST EFFORTS     21138
ENGINEER                                   14334
TEACHER                                    13990
CONSULTANT                                 13273
PROFESSOR                                  12555
Name: count, dtype: int64

contbr_occupation
RETIRED         233990
NOT PROVIDED     57151
ATTORNEY         34286
HOMEMAKER        29931
PHYSICIAN        23432
ENGINEER         14334
TEACHER          13990
CONSULTANT       13273
PROFESSOR        12555
NOT EMPLOYED      9828
Name: count, dtype: int64

party                                Democrat  Republican
contbr_occupation                                        
   MIXED-MEDIA ARTIST / STORYTELLER     100.0         NaN
 AREA VICE PRESIDENT                    250.0         NaN
 RESEARCH ASSOCIATE                     100.0         NaN
 TEACHER                                500.0         NaN
 THERAPIST                             3900.0         NaN
...                                       ...         ...
ZOOKEEPER                                35.0         NaN
ZOOLOGIST                               400.0         NaN
ZOOLOGY EDUCATION                        25.0         NaN
\NONE\                                    NaN       250.0
~                                         NaN        75.0

[45063 rows x 2 columns]

party                 Democrat   Republican
contbr_occupation                          
ATTORNEY           11141982.97   7477194.43
CEO                 2075974.79   4233741.52
CONSULTANT          2459912.71   2544725.45
ENGINEER             951525.55   1818373.70
EXECUTIVE           1355161.05   4138850.09
HOMEMAKER           4248875.80  13634275.78
INVESTOR             884133.00   2431768.92
LAWYER              3160478.87    391224.32
MANAGER              762883.22   1444532.37
NOT PROVIDED        4866973.96  20565473.01
OWNER               1001567.36   2408286.92
PHYSICIAN           3735124.94   3594320.24
PRESIDENT           1878509.95   4720923.76
PROFESSOR           2165071.08    296702.73
REAL ESTATE          528902.09   1625902.25
RETIRED            25305116.38  23561244.49
SELF-EMPLOYED        672393.40   1640252.54

<class 'pandas.core.frame.DataFrame'>
Index: 694282 entries, 411 to 701385
Data columns (total 17 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   cmte_id            694282 non-null  object 
 1   cand_id            694282 non-null  object 
 2   cand_nm            694282 non-null  object 
 3   contbr_nm          694282 non-null  object 
 4   contbr_city        694275 non-null  object 
 5   contbr_st          694278 non-null  object 
 6   contbr_zip         694234 non-null  object 
 7   contbr_employer    693607 non-null  object 
 8   contbr_occupation  693524 non-null  object 
 9   contb_receipt_amt  694282 non-null  float64
 10  contb_receipt_dt   694282 non-null  object 
 11  receipt_desc       2345 non-null    object 
 12  memo_cd            87387 non-null   object 
 13  memo_text          90672 non-null   object 
 14  form_tp            694282 non-null  object 
 15  file_num           694282 non-null  int64  
 16  party              694282 non-null  object 
dtypes: float64(1), int64(1), object(15)
memory usage: 95.3+ MB
cand_nm        contbr_occupation
Obama, Barack  RETIRED              25305116.38
               ATTORNEY             11141982.97
               NOT PROVIDED          4866973.96
               HOMEMAKER             4248875.80
               PHYSICIAN             3735124.94
               LAWYER                3160478.87
               CONSULTANT            2459912.71
Romney, Mitt   RETIRED              11508473.59
               NOT PROVIDED         11396894.84
               HOMEMAKER             8147446.22
               ATTORNEY              5364718.82
               PRESIDENT             2491244.89
               CEO                   2343547.03
               EXECUTIVE             2300947.03
Name: contb_receipt_amt, dtype: float64

411         (10, 100]
412       (100, 1000]
413       (100, 1000]
414         (10, 100]
415         (10, 100]
             ...     
701381      (10, 100]
701382    (100, 1000]
701383        (1, 10]
701384      (10, 100]
701385    (100, 1000]
Name: contb_receipt_amt, Length: 694282, dtype: category
Categories (8, interval[int64, right]): [(0, 1] < (1, 10] < (10, 100] < (100, 1000] <
                                         (1000, 10000] < (10000, 100000] < (100000, 1000000] <
                                         (1000000, 10000000]]

cand_nm              Obama, Barack  Romney, Mitt
contb_receipt_amt                               
(0, 1]                      318.24         77.00
(1, 10]                  337267.62      29819.66
(10, 100]              20288981.41    1987783.76
(100, 1000]            54798531.46   22363381.69
(1000, 10000]          51753705.67   63942145.42
(10000, 100000]           59100.00      12700.00
(100000, 1000000]       1490683.08          0.00
(1000000, 10000000]     7148839.76          0.00

cand_nm              Obama, Barack  Romney, Mitt
contb_receipt_amt                               
(0, 1]                   80.518166     19.481834
(1, 10]                  91.876684      8.123316
(10, 100]                91.076874      8.923126
(100, 1000]              71.017590     28.982410
(1000, 10000]            44.732551     55.267449
(10000, 100000]          82.311978     17.688022
(100000, 1000000]       100.000000      0.000000
(1000000, 10000000]     100.000000      0.000000
profile
+AI to AI+

0개의 댓글