python - Merge two dataframe in pandas -


i merging 2 csv(data frame) using below code:

import pandas pd = pd.read_csv(file1,dtype={'student_id': str}) df = pd.read_csv(file2) c=pd.merge(a,df,on='test_id',how='left') c.to_csv('test1.csv', index=false) 

i have following csv files

file1:

test_id, student_id 1, 01990 2, 02300 3, 05555 

file2:

test_id, result 1, pass 3, fail 

after merge

test_id, student_id , result 1, 1990, pass 2, 2300, 3, 5555, fail 

if notice student_id has 0 appended @ beginning , it's supposed considered text after merging , using to_csv function converts numeric , removes leading 0.

how can keep column "text" after to_csv?

i think to_csv function saves again numeric added dtype={'student_id': str} while reading csv.. while saving to_csv .. again convert numeric

solution join, first need read_csv parameter dtype convert student_id string , remove whitespaces skipinitialspace:

df1 = pd.read_csv(file1, dtype={'student_id': str}, skipinitialspace=true) df2 = pd.read_csv(file2, skipinitialspace=true)  df = df1.join(df2.set_index('test_id'), on='test_id') print (df)    test_id student_id  result 0        1      01990    pass 1        2      02300     nan 2        3      05555    fail 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -