0

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
mostafiz67
  • 159
  • 1
  • 9

2 Answers2

1

Just call to_frame then transpose the result:

df.mean().to_frame().T

#output
    A      B         C
0  6.0  3.333333  8.666667
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0

Use the DataFrame constructor:

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

Output:

     A         B         C
0  6.0  3.333333  8.666667
mozway
  • 194,879
  • 13
  • 39
  • 75