首页 > 解决方案 > 如何旋转并将每列的平均值转换为行

问题描述

我是 python 新手,当您将列添加为值并将值添加到行中时,我需要您的帮助来获取结果。

这是一个例子:

A   B   C
1   2   3
4   5   6
7   8   9

预期结果:

   avg
A   4
B   5
C   6

我可以通过将列放在“值”中轻松地在 excel 中做到这一点,然后移动行中的值以获得平均值,但我似乎无法在 python 中做到这一点。

标签: pythonpandas

解决方案


df=pd.DataFrame({'A':[1,4,7],'B':[2,5,8],'C':[3,6,9]})
df
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9

ser=df.mean()    #Result is a Series
df=pd.DataFrame({'avg':ser})   #Convert this Series into DataFrame
df 
   avg
A  4.0
B  5.0
C  6.0

推荐阅读