Home > Back-end >  Merge 2 CSV files with no common column
Merge 2 CSV files with no common column

Time:11-22

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

CodePudding user response:

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).

  • Related