Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: How to merge multiple dataframes with same column names on one column?

Tags:

python

pandas

I have N dataframes:

df1:
time  data
1.0   a1
2.0   b1
3.0   c1


df2:
time  data
1.0   a2
2.0   b2
3.0   c2



df3:
time  data
1.0   a3
2.0   b3
3.0   c3

I want to merge all of them on id, thus getting

time  data1    data2    data3
1.0   a1       a2       a3
2.0   b1       b2       b3
3.0   c1       c2       c3

I can assure all the ids are the same in all dataframes.

How can I do this in pandas?

like image 971
Gulzar Avatar asked Oct 28 '25 17:10

Gulzar


1 Answers

One idea is use concat for list of DataFrames - only necessary create index by id for each DaatFrame. Also for avoid duplicated columns names is added keys parameter, but it create MultiIndex in output. So added map with format for flatten it:

dfs = [df1, df2, df3]
dfs = [x.set_index('id') for x in dfs]

df = pd.concat(dfs, axis=1, keys=range(1, len(dfs) + 1))
df.columns = df.columns.map('{0[1]}{0[0]}'.format)
df = df.reset_index()
print (df)
   id data1 data2 data3
0   1    a1    a2    a3
1   2    b1    b2    b3
2   3    c1    c2    c3
like image 120
jezrael Avatar answered Oct 31 '25 10:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!