Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Analysis with Python——09 #89

Open
hsipeng opened this issue Aug 23, 2019 · 1 comment
Open

Data Analysis with Python——09 #89

hsipeng opened this issue Aug 23, 2019 · 1 comment

Comments

@hsipeng
Copy link
Owner

hsipeng commented Aug 23, 2019

Data Analysis with Python——09

GroupBy 实例

import pandas as pd
from pandas import DataFrame, Series
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

fec = pd.read_csv('code/pydata-book/datasets/fec/P00000001-ALL.csv')

fec
fec.loc[123456]
#fec.ix[123456]

unique_cands = fec.cand_nm.unique()
unique_cands

parties={'Bachmann, Michelle':'Republican','Cain, Herman':'Republican','Gingrich, Newt':'Republican','Huntsman, Jon':'Republican','Johnson, GaryEarl':'Republican','McCotter, ThaddeusG':'Republican','Obama, Barack':'Democrat','Paul, Ron':'Republican','Pawlenty, Timothy':'Republican','Perry, Rick':'Republican',"Roemer, CharlesE. 'Buddy' III":'Republican','Romney, Mitt':'Republican','Santorum, Rick':'Republican'}

fec.cand_nm[123456:123461].map(parties)


# 加入'party' 属性
fec['party'] = fec.cand_nm.map(parties)

fec['party'].value_counts()


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

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

fec_mrbo



fec.contbr_occupation.value_counts()[:10]


# 职业转换
occ_mapping={'INFORMATION REQUESTED PER BEST EFFORTS':'NOT PROVIDED','INFORMATION REQUESTED':'NOT PROVIDED','INFORMATION REQUESTED (BEST EFFORTS)':'NOTPROVIDED','C.E.O.':'CEO'}

f = lambda x: occ_mapping.get(x, x)

fec.contbr_occupation = fec.contbr_occupation.map(f)

emp_mapping={'INFORMATION REQUESTED PER BEST EFFORTS':'NOT PROVIDED','INFORMATION REQUESTED':'NOT PROVIDED','SELF':'SELF EMPLOYED','SELF-EMPLOYED':'SELF-EMPLOYED',}

f2 = lambda x: emp_mapping.get(x, x)

fec.contbr_employer = fec.contbr_employer.map(f2)

by_occupation = fec.pivot_table('contb_receipt_amt', index='contbr_occupation', columns='party',aggfunc='sum')
#对各党派总出资额最高的职业
over_2mm = by_occupation[by_occupation.sum(1) > 2000000]
over_2mm



over_2mm.plot(kind='barh')
#对各党派总出资额最高的职业
@hsipeng
Copy link
Owner Author

hsipeng commented Aug 23, 2019

资料集

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant