Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two csv files using pandas

Can anyone check for me what's wrong with my code. I want it merge two csv file into one csv file.

I hve tried to google and I still cant merge it, it will create new file but will show nothing inside. https://stackoverflow.com/a/16266144/7624469

a.csv

ID    User
A1    Fi
A2    Ki

b.csv

ID    User
A4    Fsdi
A5    Kisd

The output that I want will look like this

combined.csv

ID    User
A1    Fi
A2    Ki
A4    Fsdi
A5    Kisd

test.py

import pandas, sys
import pandas as pd


a = pd.read_csv("C:/JIRA Excel File/a.csv")
b = pd.read_csv("C:/JIRA Excel File/b.csv")

merged = a.merge(b, on='ID')

merged.to_csv('C:/JIRA Excel File/result.csv', index=False)
like image 832
Fiqri Mfbw Avatar asked Sep 06 '17 07:09

Fiqri Mfbw


People also ask

How do I merge two datasets in pandas?

Pandas DataFrame merge() function is used to merge two DataFrame objects with a database-style join operation. The joining is performed on columns or indexes. If the joining is done on columns, indexes are ignored. This function returns a new DataFrame and the source DataFrame objects are unchanged.


2 Answers

Using df.append:

out = df1.append(df2)
print(out)

   ID  User
0  A1    Fi
1  A2    Ki
0  A4  Fsdi
1  A5  Kisd

with open('C:/JIRA Excel File/result.csv', 'w', encoding='utf-8') as f:
    out.to_csv(f, index=False)
like image 167
cs95 Avatar answered Oct 06 '22 13:10

cs95


It is better to use pd.concat here to combine this frames, not merge:

merged = pd.concat([a,b])

Toy example with your data:

a = pd.DataFrame([['Fi'],['Ki']],columns=['User'], index=['A1','A2'],) #'ID')
b = pd.DataFrame([['Fi'],['Ki']],columns=['User'], index=['A4','A5'],) #'ID')
pd.concat([a,b])

Will output:

    User
A1  Fi
A2  Ki
A4  Fi
A5  Ki
like image 10
Grigory Avatar answered Oct 06 '22 13:10

Grigory