我在python中处理它时有一个CSV文件我面临以下问题:
CSV文件:
cand_id cand_name cand_age cand_sex
A1 Adam 35 M
A2 Max 31 M
A3 Uma 32 F
B1 Jack 29 M
B2 Maya 30 F
现在在python中加载后,out文件就变成了这样:
cand_id cand_name cand_age cand_sex
Adam 35 M NaN
Max 31 M NaN
Uma 32 F NaN
Jack 29 M NaN
Maya 30 F Nan
请告诉我如何将正确的列名称与相应的数据对齐。
由于
答案 0 :(得分:1)
您需要将参数index_col=False
添加到read_csv
:
import pandas as pd
df = pd.read_csv('P00000001-AL.csv', index_col=False)
print (df.head())
cmte_id cand_id cand_nm contbr_nm \
0 C00574624 P60006111 Cruz, Rafael Edward 'Ted' LUCAS, FRAN
1 C00574624 P60006111 Cruz, Rafael Edward 'Ted' KERR, JOHN MCCLURE II
2 C00574624 P60006111 Cruz, Rafael Edward 'Ted' LADD, TEENA E. MRS.
3 C00574624 P60006111 Cruz, Rafael Edward 'Ted' KERR, JOHN MCCLURE II
4 C00574624 P60006111 Cruz, Rafael Edward 'Ted' KERR, JOHN MCCLURE II
contbr_city contbr_st contbr_zip contbr_employer \
0 FAIRHOPE AL 365322922.0 SELF EMPLOYED
1 HAMILTON AL 355706637.0 NORTH MISSISSIPPI MEDICAL CENTER
2 MADISON AL 357586884.0 RETIRED
3 HAMILTON AL 355706637.0 NORTH MISSISSIPPI MEDICAL CENTER
4 HAMILTON AL 355706637.0 NORTH MISSISSIPPI MEDICAL CENTER
contbr_occupation contb_receipt_amt contb_receipt_dt \
0 COSMETOLOGIST 25.0 27-APR-16
1 PHYSICIAN 1000.0 28-MAR-16
2 RETIRED 25.0 20-APR-16
3 PHYSICIAN -100.0 30-APR-16
4 PHYSICIAN 100.0 30-APR-16
receipt_desc memo_cd memo_text form_tp \
0 NaN NaN NaN SA17A
1 SEE REDESIGNATION X SEE REDESIGNATION SA17A
2 NaN NaN NaN SA17A
3 REDESIGNATION TO GENERAL X REDESIGNATION TO GENERAL SA17A
4 REDESIGNATION FROM PRIMARY X REDESIGNATION FROM PRIMARY SA17A
file_num tran_id election_tp
0 1077664 SA17A.1722559 P2016
1 1077664 SA17A.1675656 P2016
2 1077664 SA17A.1693960 P2016
3 1077664 SA17A.1827542 P2016
4 1077664 SA17A.1827677 G2016
通过评论编辑:
print (df)
cand_id cand_name cand_age cand_sex
0 A1 Adam 35 M
1 A2 Max 31 M
2 A3 Uma 32 F
3 B1 Jack 29 M
4 B2 Maya 30 F
print (df.ix[2])
cand_id A3
cand_name Uma
cand_age 32
cand_sex F
Name: 2, dtype: object
df.set_index('cand_id', inplace=True)
print (df.ix['A3'])
cand_name Uma
cand_age 32
cand_sex F
Name: A3, dtype: object