Search code examples
pythondataframedata-manipulation

Add the dataframes by a group and divide by that number of occurences


Lets say i have the following dataframes of different lengths:

  name  value
0    a    4.0
1    b    6.0
2    c    5.0
  name  value
0    a    4.0
1    b    2.0
  name  value
0   c    3.0

how would I add them together and then divide by the amount of occurences? Final dataframe below:

  name  value
0    a    4.0 = (4+4)/2 
1    b    4.0 = (6+2)/2
2    c    2.67 = (5+3)/3

Thank you again for your help.


Solution

  • Let's say you have 3 DataFrames you can concat them together, take a groupby and return mean.

    df = pd.concat([df1, df2, df3])
    df.groupby('name')['value'].mean().reset_index()