ChauhanV ChauhanV - 4 months ago 9
Python Question

Column name and corresponding data are not matching in python

I have a CSV file n while working on it in python I am facing following problem:

CSV file:

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


Now after loading it in python, the out file has become something like this:

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


please tell me how can I align the correct column name with the corresponding data.

Thanks

Answer

You need add parameter index_col=False to 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  

EDIT by comment:

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
Comments