pandasdataframedata-sciencedata-munging

Pandas get mean value per (row,col) across list of dataframes


I have a dictionary of dataframes:

{1 : df1, 2 : df2 .. }

All dataframe are with the same shapes. (but different number of rows). I want to create the dataframe where every column is the mean of this column for this row. So if:

df1 : A  B  C
      2  4  6
      1  3  5
df2 : A  B  C
      0  2  8
      7  9  5

I will get:

new_df:  A  B  C 
         1  3  7
         4  6  5

What is the best way to do so?


Solution

  • Try via concat() and mean():

    out=pd.concat(d.values()).mean(level=0)
    

    OR

    out=pd.concat(d).mean(level=1)
    

    Note: here d is your dictionary of dataframes

    output of out:

        A   B   C
    0   1   3   7
    1   4   6   5