lcut函数python pythonljust函数( 四 )


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

推荐阅读