美国大选献金项目数据分析

需求

  • 加载数据
  • 查看数据的基本信息
  • 指定数据截取,将如下字段的数据进行提取,其他数据舍弃
    • 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主要支持谁

import pandas as pd
from pandas import DataFrame
import numpy as np

df = pd.read_csv(‘./usa_election.csv’)
df.head()
在这里插入图片描述
在这里插入图片描述

df = df[[‘cand_nm’,‘contbr_nm’,‘contbr_st’,‘contbr_employer’,‘contbr_occupation’,‘contb_receipt_amt’,‘contb_receipt_dt’]]
df.head(5)
在这里插入图片描述

#对新数据进行总览,查看是否存在缺失数据
df.info()
在这里插入图片描述

#用统计学指标快速描述数值型属性的概要。
df.describe() #默认情况下是对df中的数值型数据进行统计描述
在这里插入图片描述

#对非数值型数据进行统计描述
df[[‘cand_nm’,‘contbr_nm’,‘contbr_st’,‘contbr_employer’,‘contbr_occupation’,‘contb_receipt_dt’]].describe()
在这里插入图片描述

#空值处理。可能因为忘记填写或者保密等等原因,相关字段出现了空值,将其填充为NOT PROVIDE
df.fillna(value=‘NOT PROVIDE’,inplace=True)

#异常值处理。将捐款金额<=0的数据删除
df.loc[df[‘contb_receipt_amt’] <= 0] #获取了异常数据对应的行数据
indexs = df.loc[df[‘contb_receipt_amt’] <= 0].index #获取了异常数据对应的行索引
df.drop(index=indexs,inplace=True)

#新建一列为各个候选人所在党派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)

#查看party这一列中有哪些不同的元素
df[‘party’].unique()
array([‘Republican’, ‘Democrat’, ‘Reform’, ‘Libertarian’], dtype=object)
#统计party列中各个元素出现次数
df[‘party’].value_counts()
在这里插入图片描述

#查看各个党派收到的政治献金总数contb_receipt_amt
df.groupby(by=‘party’)[‘contb_receipt_amt’].sum()
在这里插入图片描述

#注意:在Series中调用一个reset_index()可以将其快速变为df表格的样式
df.groupby(by=‘party’)[‘contb_receipt_amt’].sum().reset_index()
在这里插入图片描述

df.pivot_table(index=‘party’,values=‘contb_receipt_amt’,aggfunc=‘sum’)
在这里插入图片描述

#查看具体每天各个党派收到的政治献金总数contb_receipt_amt
df.groupby(by=[‘contb_receipt_dt’,‘party’])[‘contb_receipt_amt’].sum()
在这里插入图片描述

ret = df.pivot_table(index=[‘contb_receipt_dt’,‘party’],values=‘contb_receipt_amt’,aggfunc=‘sum’)
ret
在这里插入图片描述

#将表中日期格式转换为’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 transform_date(d):
day,month,year = d.split(‘-’)
month = months[month]
return ‘20’+year+‘-’+str(month)+‘-’+day
df[‘contb_receipt_dt’] = df[‘contb_receipt_dt’].map(transform_date)

#查看老兵(捐献者职业)DISABLED VETERAN主要支持谁
data = df.loc[df[‘contbr_occupation’] == ‘DISABLED VETERAN’]#取出了老兵对应的行数据
#分析老兵给哪位候选人捐赠的金额是最多的
data.groupby(by=‘cand_nm’)[‘contb_receipt_amt’].sum().sort_values(ascending=False)
在这里插入图片描述

#分析老兵给哪位候选人捐赠的次数是最多的
#size用来统计每组数据的行数
data.groupby(by=‘cand_nm’).size().sort_values(ascending=False)
在这里插入图片描述
内容来自大数据分析课程。

  • 12
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ak2111

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值