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)
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With