3-python数据分析-pandas高级操作巩固之2012美国大选献金项目数据分析
需求
- 加载数据
- 查看数据的基本信息
- 指定数据截取,将如下字段的数据进行提取,其他数据舍弃
- cand_nm :候选人姓名
- contbr_nm : 捐赠人姓名
- contbr_st :捐赠人所在州
- contbr_employer : 捐赠人所在公司
- contbr_occupation : 捐赠人职业
- contb_receipt_amt :捐赠数额(美元)
- contb_receipt_dt : 捐款的日期
- 对新数据进行总览,查看是否存在缺失数据
- 用统计学指标快速描述数值型属性的概要。
- 空值处理。可能因为忘记填写或者保密等等原因,相关字段出现了空值,将其填充为NOT PROVIDE
- 异常值处理。将捐款金额<=0的数据删除
- 新建一列为各个候选人所在党派party
- 查看party这一列中有哪些不同的元素
- 统计party列中各个元素出现次数
- 查看各个党派收到的政治献金总数contb_receipt_amt
- 查看具体每天各个党派收到的政治献金总数contb_receipt_amt
- 将表中日期格式转换为'yyyy-mm-dd'。
- 查看老兵(捐献者职业)DISABLED VETERAN主要支持谁
1. 加载数据
df = pd.read_csv('./data/usa_election.txt') df.head()
2. 查看数据的基本信息
df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 536041 entries, 0 to 536040 Data columns (total 16 columns): cmte_id 536041 non-null object cand_id 536041 non-null object cand_nm 536041 non-null object contbr_nm 536041 non-null object contbr_city 536026 non-null object contbr_st 536040 non-null object contbr_zip 535973 non-null object contbr_employer 525088 non-null object contbr_occupation 530520 non-null object contb_receipt_amt 536041 non-null float64 contb_receipt_dt 536041 non-null object receipt_desc 8479 non-null object memo_cd 49718 non-null object memo_text 52740 non-null object form_tp 536041 non-null object file_num 536041 non-null int64 dtypes: float64(1), int64(1), object(14) memory usage: 65.4+ MB
3. 指定数据截取
df=df[['cand_nm','contbr_nm','contbr_st','contbr_employer','contbr_occupation','contb_receipt_amt','contb_receipt_dt']] df.head()
4. 对新数据进行总览,查看是否存在缺失数据
df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 536041 entries, 0 to 536040 Data columns (total 7 columns): cand_nm 536041 non-null object contbr_nm 536041 non-null object contbr_st 536040 non-null object contbr_employer 525088 non-null object contbr_occupation 530520 non-null object contb_receipt_amt 536041 non-null float64 contb_receipt_dt 536041 non-null object dtypes: float64(1), object(6) memory usage: 28.6+ MB
5. 用统计学指标快速描述数值型属性的概要
df.describe() # 对原始数据数值型数据的统计描述
6. 空值处理。可能因为忘记填写或者保密等等原因,相关字段出现了空值,将其填充为NOT PROVIDE
df.fillna(value='NOT PROVIDE', inplace=True)
7. 异常值处理。将捐款金额<=0的数据删除
df = df.loc[~(df['contb_receipt_amt'] <= 0)]
8. 新建一列为各个候选人所在党派party
parties = { 'Bachmann, Michelle': 'Republican', 'Romney, Mitt': 'Republican', 'Obama, Barack': 'Democrat', "Roemer, Charles E. 'Buddy' III": 'Reform', 'Pawlenty, Timothy': 'Republican', 'Johnson, Gary Earl': 'Libertarian', 'Paul, Ron': 'Republican', 'Santorum, Rick': 'Republican', 'Cain, Herman': 'Republican', 'Gingrich, Newt': 'Republican', 'McCotter, Thaddeus G': 'Republican', 'Huntsman, Jon': 'Republican', 'Perry, Rick': 'Republican' } df['party'] = df['cand_nm'].map(parties)
9. 查看party这一列中有哪些不同的元素
df['party'].unique() array(['Republican', 'Democrat', 'Reform', 'Libertarian'], dtype=object)
10. 统计party列中各个元素出现次数
df['party'].value_counts() Democrat 289999 Republican 234300 Reform 5313 Libertarian 702 Name: party, dtype: int64
11. 查看各个党派收到的政治献金总数contb_receipt_amt
df.groupby(by='party')['contb_receipt_amt'].sum() party Democrat 8.259441e+07 Libertarian 4.132769e+05 Reform 3.429658e+05 Republican 1.251181e+08 Name: contb_receipt_amt, dtype: float64
12. 查看具体每天各个党派收到的政治献金总数contb_receipt_amt
# 俩种方法一种用groupby,一种透视表pivot_table # 分组groupby df.groupby(by=['contb_receipt_dt','party'])['contb_receipt_amt'].sum() # 一种透视表pivot_table df.pivot_table(index='contb_receipt_dt', values='contb_receipt_amt',aggfunc='sum', columns='party',fill_value=0)
13. 将表中日期格式转换为'yyyy-mm-dd'
months = {'JAN' : 1, 'FEB' : 2, 'MAR' : 3, 'APR' : 4, 'MAY' : 5, 'JUN' : 6, 'JUL' : 7, 'AUG' : 8, 'SEP' : 9, 'OCT': 10, 'NOV': 11, 'DEC' : 12} def change_date(s): day,month,year = s.split('-') year = '20'+ year month = str(months[month]) return year + '-' + month + '-'+ day # map和apply都行,但apply效率更高 df['contb_receipt_dt'] = df['contb_receipt_dt'].apply(change_date)
14. 查看老兵(捐献者职业)DISABLED VETERAN主要支持谁
# 老兵给谁捐的钱最多就表示最支持谁 # 1.将老兵对应的行数据 old_bing_df = df.loc[df['contbr_occupation'] == 'DISABLED VETERAN'] # 2.根据候选人分组 old_bing_df.groupby(by='cand_nm')['contb_receipt_amt'].sum() cand_nm Cain, Herman 300.00 Obama, Barack 4205.00 # 他的现金最多,老兵最支持他 Paul, Ron 2425.49 Santorum, Rick 250.00 Name: contb_receipt_amt, dtype: float64