Home > Software engineering >  How to store dataframe mean as a column wise
How to store dataframe mean as a column wise

Time:11-13

If we have a dataframe like the below one

    A   B   C
0   5   3   8
1   5   3   9
2   8   4   9

We can calculate the mean using df.mean() and the output looks like

A    6.000000
B    3.333333
C    8.666667
dtype: float64

Now, I want to save the mean in a column-wise format like the below one.

     A     B     C
0   6.0   3.3   8.6

How can I do this?

I have gone through a couple of posts but have not gotten any idea and the sample data was taken from this post.

CodePudding user response:

Just call to_frame then transpose the result:

df.mean().to_frame().T

#output
    A      B         C
0  6.0  3.333333  8.666667

CodePudding user response:

Use the DataFrame constructor:

out = pd.DataFrame([df.mean()])

Output:

     A         B         C
0  6.0  3.333333  8.666667
  • Related