Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas merge two dataframes by taking the mean between the columns

I am working with Pandas DataFrames and looking to take the mean between two of them. I am looking to take the mean between columns with the same names. For example

df1

    time     x    y     z
 0     1  1.25  2.5  0.75
 1     2  2.75  2.5  3.00
 2     3  1.50  2.5  1.25
 3     4  3.00  2.5  3.50
 4     5  0.50  2.5  2.25

df2

    time     x    y     z
 0     2  0.75  2.5  1.75
 1     3  3.00  2.5  3.00
 2     4  1.25  2.5  0.25
 3     5  3.50  2.5  2.00
 4     6  2.25  2.5  2.25

and the result I am looking for is

df3

    time     x    y     z
 0     1  1.25  2.5  0.75
 1     2  1.75  2.5  2.375
 2     3  2.25  2.5  2.125
 3     4  2.125 2.5  1.875
 4     5  2.00  2.5  2.125
 5     6  2.25  2.5  2.25

Is there a simple way in Pandas that I can do this, using the merge function or similar? I am looking for a way of doing it without having to specify the name of the columns.

like image 532
candid Avatar asked Sep 12 '25 10:09

candid


1 Answers

I think you need concat + groupby and aggregate mean:

df = pd.concat([df1, df2]).groupby('time', as_index=False).mean()
print (df)
   time      x    y      z
0     1  1.250  2.5  0.750
1     2  1.750  2.5  2.375
2     3  2.250  2.5  2.125
3     4  2.125  2.5  1.875
4     5  2.000  2.5  2.125
5     6  2.250  2.5  2.250
like image 140
jezrael Avatar answered Sep 14 '25 01:09

jezrael