Join 2 CSV with Pandas

Viewed 8709

I have 2 CSV (emails1.csv and emails2.csv)

What i need is Join these 2 CSV into one, because they are too big for work with excel.

I need to export to CSV and TXT.

What i did is create a Python file:

import csv
import pandas as pd
import numpy as np

df1 = pd.read_csv("emails1.csv")
df2 = pd.read_csv("emails2.csv")

df3 = pd.merge(df1, df2, on=["email"])
df3.to_csv("final.csv",index=False)

The CSV only have the email column

Thanks for the help.

1 Answers

You are missing out on how to join the two dataframes. I just made a small adjustment to your given code and it works perfectly.

import csv
import pandas as pd
import numpy as np

df1 = pd.read_csv("emails1.csv")
df2 = pd.read_csv("emails2.csv")

df3 = df1.merge(df2, on=["email"], how='outer')
df3.to_csv("final.csv",index=False)

Please note the how parameter, and the way merge is called.

This is emails1.csv :

email
one@gmail.com
two@gmail.com

This is emails2.csv :

email
three@gmail.com
four@gmail.com

And this is the final.csv after executing my code:

email
one@gmail.com
two@gmail.com
three@gmail.com
four@gmail.com

I hope this is what you wanted. :-) cheers!

Related