gpt4 book ai didi

python - 列名和相应的数据在 python 中不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 05:26:30 26 4
gpt4 key购买 nike

我在用 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

请告诉我如何将正确的列名与相应的数据对齐。

谢谢

最佳答案

您需要将参数 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

关于python - 列名和相应的数据在 python 中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38495355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com