Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge 2 CSV files with no common column

I have 2 csv files( 2 million each) with below structure

first.csv
h1,h2
2,3
4,5
second.csv
h3,h4
5,6
7,8

I want to merge these 2 csv index wise column like below

merged.csv
h1,h2,h3,h4
2,3,5,6
4,5,7,8

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You might be looking for the pandas.concat() function (see here). Here is an example:

import pandas as pd

df1 = pd.DataFrame({'A':[0,0,1,1,2],'B':[3,2,5,3,5]})
df2 = pd.DataFrame({'C':[0,0,1,1,2],'D':[-1,20,-2,33,17]})

df3 = pd.concat((df1,df2),axis=1)

df3.to_csv('myFile.csv')

You just have to replace df1 and df2 by your csv files using the pandas.read_csv function (here).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading