python分析奥巴马资金来源奥巴马的竞选资金是一点点从选民那里募集来的 。如获党内提名,可得政府拔款,但也没多少 。美国大选不仅禁外国人捐款,而且禁止公司机构捐款,而只允许个人捐款 。不仅如此,还为个人捐款限制了上限,防止富人捐过多的款而影响未来的公平执政 。
不仅富人自己不能多捐,如果某个老板呼吁自己的员工给某人捐钱或投票支持他,都是犯法的 。因此,想要筹到几千万竞争资金 , 唯一的办法是争取更多选民支持 , 一点点募集 。所以,中国、公司、大笔捐款,这三条都是犯法的 。
我记得以前已经有华人闹过这种丑闻了 。美国的选举法就是要严防少数人企图用几个臭钱影响美国的政治 。所以我们作为外国人就更别去自讨没趣了 。
导入包
In [1]:
import numpy as npimport pandas as pdfrom pandas import Series,DataFrame
方便大家操作,将月份和参选人以及所在政党进行定义
In [2]:
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}of_interest = ['Obama, Barack', 'Romney, Mitt', 'Santorum, Rick','Paul, Ron', 'Gingrich, Newt']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'}
读取文件
In [3]:
table = pd.read_csv('data/usa_election.txt')table.head()
C:\jupyter\lib\site-packages\IPython\core\interactiveshell.py:2785: DtypeWarning: Columns (6) have mixed types. Specify dtype option on import or set low_memory=False.interactivity=interactivity, compiler=compiler, result=result)
Out[3]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 20-JUN-11 NaN NaN NaN SA17A 736166
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 23-JUN-11 NaN NaN NaN SA17A 736166
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 05-JUL-11 NaN NaN NaN SA17A 749073
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 01-AUG-11 NaN NaN NaN SA17A 749073
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 20-JUN-11 NaN NaN NaN SA17A 736166
In [8]:
#使用map函数 字典,新建一列各个候选人所在党派partytable['party'] = table['cand_nm'].map(parties)table.head()
Out[8]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 20-JUN-11 NaN NaN NaN SA17A 736166 Republican
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 23-JUN-11 NaN NaN NaN SA17A 736166 Republican
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 05-JUL-11 NaN NaN NaN SA17A 749073 Republican
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 01-AUG-11 NaN NaN NaN SA17A 749073 Republican
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 20-JUN-11 NaN NaN NaN SA17A 736166 Republican
In [10]:
#party这一列中有哪些元素table['party'].unique()
Out[10]:
array(['Republican', 'Democrat', 'Reform', 'Libertarian'], dtype=object)
In [ ]:
#使用value_counts()函数,统计party列中各个元素出现次数 , value_counts()是Series中的,无参,返回一个带有每个元素出现次数的Series
In [11]:
table['party'].value_counts()
Out[11]:
Democrat292400Republican237575Reform5364Libertarian702Name: party, dtype: int64
In [12]:
#使用groupby()函数,查看各个党派收到的政治献金总数contb_receipt_amttable.groupby(by='party')['contb_receipt_amt'].sum()
Out[12]:
partyDemocrat8.105758e 07Libertarian4.132769e 05Reform3.390338e 05Republican1.192255e 08Name: contb_receipt_amt, dtype: float64
In [13]:
#查看具体每天各个党派收到的政治献金总数contb_receipt_amt。使用groupby([多个分组参数])table.groupby(by=['party','contb_receipt_dt'])['contb_receipt_amt'].sum()
Out[13]:
partycontb_receipt_dtDemocrat01-AUG-11175281.0001-DEC-11651532.8201-JAN-1258098.8001-JUL-11165961.0001-JUN-11145459.0001-MAY-1182644.0001-NOV-11122529.8701-OCT-11148977.0001-SEP-11403297.6202-AUG-11164510.1102-DEC-11216056.9602-JAN-1289743.6002-JUL-1117105.0002-JUN-11422453.0002-MAY-11396675.0002-NOV-11147183.8102-OCT-1162605.6202-SEP-11137948.4103-AUG-11147053.0203-DEC-1181304.0203-JAN-1287406.9703-JUL-115982.0003-JUN-11320176.2003-MAY-11261819.1103-NOV-11119304.5603-OCT-11363061.0203-SEP-1145598.0004-APR-11640235.1204-AUG-11598784.2304-DEC-1172795.10...Republican29-AUG-11941769.2329-DEC-11428501.4229-JAN-11750.0029-JAN-1275220.0229-JUL-11233423.3529-JUN-111340704.2929-MAR-1138875.0029-MAY-118363.2029-NOV-11407322.6429-OCT-1181924.0129-SEP-111612794.5230-APR-1143004.8030-AUG-11915548.5830-DEC-11492470.4530-JAN-12255204.8030-JUL-1112249.0430-JUN-112744932.6330-MAR-1150240.0030-MAY-1117803.6030-NOV-11809014.8330-OCT-1143913.1630-SEP-114886331.7631-AUG-111017735.0231-DEC-111094376.7231-JAN-116000.0031-JAN-12869890.4131-JUL-1112781.0231-MAR-1162475.0031-MAY-11301339.8031-OCT-11734601.83Name: contb_receipt_amt, Length: 1183, dtype: float64
In [14]:
def trasform_date(d):day,month,year = d.split('-')month = months[month]return "20" year '-' str(month) '-' day
In [17]:
#将表中日期格式转换为'yyyy-mm-dd' 。日期格式,通过函数加map方式进行转换table['contb_receipt_dt'] = table['contb_receipt_dt'].apply(trasform_date)
In [18]:
table.head()
Out[18]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 2011-6-20 NaN NaN NaN SA17A 736166 Republican
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 2011-6-23 NaN NaN NaN SA17A 736166 Republican
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 2011-7-05 NaN NaN NaN SA17A 749073 Republican
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 2011-8-01 NaN NaN NaN SA17A 749073 Republican
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 2011-6-20 NaN NaN NaN SA17A 736166 Republican
In [19]:
#查看老兵(捐献者职业)DISABLED VETERAN主要支持谁:查看老兵们捐赠给谁的钱最多table['contbr_occupation'] == 'DISABLED VETERAN'
Out[19]:
0False1False2False3False4False5False6False7False8False9False10False11False12False13False14False15False16False17False18False19False20False21False22False23False24False25False26False27False28False29False...536011False536012False536013False536014False536015False536016False536017False536018False536019False536020False536021False536022False536023False536024False536025False536026False536027False536028False536029False536030False536031False536032False536033False536034False536035False536036False536037False536038False536039False536040FalseName: contbr_occupation, Length: 536041, dtype: bool
In [21]:
old_bing_df = table.loc[table['contbr_occupation'] == 'DISABLED VETERAN']
In [22]:
old_bing_df.groupby(by='cand_nm')['contb_receipt_amt'].sum()
Out[22]:
cand_nmCain, Herman300.00Obama, Barack4205.00Paul, Ron2425.49Santorum, Rick250.00Name: contb_receipt_amt, dtype: float64
In [23]:
table['contb_receipt_amt'].max()
Out[23]:
1944042.43
In [24]:
#找出候选人的捐赠者中,捐赠金额最大的人的职业以及捐献额.通过query("查询条件来查找捐献人职业")table.query('contb_receipt_amt == 1944042.43')
Out[24]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
176127 C00431445 P80003338 Obama, Barack OBAMA VICTORY FUND 2012 - UNITEMIZED CHICAGO IL 60680 NaN NaN 1944042.43 2011-12-31 NaN X * SA18 763233 Democrat
来源:
Python如何重叠图片?图片叠加再一起成这种形式(batch,28,28,1)
可以使用numpy库的concatenate函数实现
import numpy as np
a = np.array([[0,1]])
print(a.shape)
b = np.array([[0,1]])
print(b.shape)
print (np.concatenate((a,b),axis = 0).shape)
输出如下:
python 中 inspect模块的stack函数有阶乘函数:
12improt numpyprint numpy.math.factorial(3)
python 自带的标准库也有阶乘函数
【python堆叠图函数 python堆叠瀑布图怎么做】12import mathprint math.factorial(3)
输出是6
python堆叠图函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python堆叠瀑布图怎么做、python堆叠图函数的信息别忘了在本站进行查找喔 。
推荐阅读
- 新人直播游戏教程,新人直播游戏不冷场的技巧
- 单机群侠传ios武功威力,单机群侠传小虾米武功激活
- 枪战模拟器游戏介绍大全,枪战模拟器小游戏
- 怎么联系上门修路由器电话,路由器维修上门服务
- java实用代码合集 常用java代码
- python安装老丢失文件,python下载后找不到安装包
- sqlserver用户登录日志,sql server的登录账户信息保存在哪
- watch3匹配多少ios,apple watch3配对教程
- python内建函数总结 python 内建函数