数据分析-以美国2012大选为例

导入库:

import numpy as np
import pandas as pd

再导入文件:

td=pd.read_csv('./usa_election.txt')
td

在这里插入图片描述
在这里插入图片描述
分析数据

td.info()

在这里插入图片描述
分析纯数字的列

td.describe()

在这里插入图片描述

发现有空值,由于保密或者其他原因造成了空值,所以需要用关键字’NOT PR0VIDE’进行填充。

td.fillna(value='NOT PR0VIDE',inplace=True)

查看文件发现有些’contb_receipt_amt’(捐赠金额)为负,需要删除。

td['contb_receipt_amt']<0
td.loc[td['contb_receipt_amt']<0]
drop_indexs=td.loc[td['contb_receipt_amt']<0].index
td.drop(labels=drop_indexs,axis=0,inplace=True)

将日期格式转换为yyyy-mm-dd的格式。
首先建立字典

months={'JAN':'01',
        'FEB':'02',
        'MAR':'03',
        'APR':'04',
        'MAY':'05',
        'JUN':'06',
        'JUL':'07',
        'AUG':'08',
        'SEP':'09',
        'OCT':'10',
        'NOV':'11',
        'DEC':'12'}

然后处理

def transformData(f):
    day,month,year=f.split('-')
    month=months[month]
    return '20'+year+'-'+month+'-'+day
td['contb_receipt_dt']=td['contb_receipt_dt'].map(transformData)

查看老兵最支持谁:

td['contbr_occupation']=='DISABLED VETERAN'
veteran=td.loc[td['contbr_occupation']=='DISABLED VETERAN']
veteran.groupby(by='cand_nm')['contb_receipt_amt'].sum()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值