Skip to content Skip to sidebar Skip to footer

Element-wise Mean Of A List Of Pandas Dataframes

Is there a canonical way to compute the element-wise mean of a list of DataFrames with identical columns and indices? The best way I can think of is from functools import reduce

Solution 1:

Use concat with mean per index values:

df1 = pd.DataFrame({
         'C':[7,8,9],
         'D':[1,3,5],

})
df2 = pd.DataFrame({
         'C':[4,2,3],
         'D':[7,1,0],

})
df3 = pd.DataFrame({
         'C':[9,4,2],
         'D':[1,7,1],

})

from functools import reduce

dfs = [df1, df2, df3]  
df = reduce(lambda x, y: x.add(y), dfs) / len(dfs)
print (df)
          C         D
0  6.666667  3.000000
1  4.666667  3.666667
2  4.666667  2.000000

df = pd.concat(dfs).mean(level=0)
print (df)
          C         D
0  6.666667  3.000000
1  4.666667  3.666667
2  4.666667  2.000000

Post a Comment for "Element-wise Mean Of A List Of Pandas Dataframes"