Pranayjeet Thakare
Pranayjeet Thakare

Reputation: 634

Pandas multi column mean

I have a pandas DataFrame, and would like to get column wise mean() as below.

    A    B    C      D
1   10  100 1000    10000
2   20  200 2000    20000
3   30  300 3000    30000
4   40  400 4000    40000
5   50  500 5000    50000

Answer:

  A    B      C      D
  30  300   3000    30000

Please suggest a way to do it. I have tried df.mean() and other variations of it.

Upvotes: 1

Views: 374

Answers (1)

jezrael
jezrael

Reputation: 863801

Add to_frame with T:

print (df.mean().to_frame().T)
      A      B       C        D
0  30.0  300.0  3000.0  30000.0

Or:

print (pd.DataFrame(df.mean().values.reshape(1,-1), columns=df.columns))
      A      B       C        D
0  30.0  300.0  3000.0  30000.0

Or:

print (pd.DataFrame(np.mean(df.values, axis=0).reshape(1,-1), columns=df.columns))
      A      B       C        D
0  30.0  300.0  3000.0  30000.0

Upvotes: 3

Related Questions