我正在尝试为平均列添加列标题。引用下文 df 中每列的平均值:
A B C
0 5 3 8
1 5 3 9
2 8 4 9
df.mean()
A 6.000000
B 3.333333
C 8.666667
dtype: float64
我想要实现的是以下输出:
Mean
A 6.000000
B 3.333333
C 8.666667
dtype: float64
到目前为止,我已经尝试过 df.columns['Mean']
但不起作用。我认为这与 dtype float64 而不是标准数据帧对象有关。谁能指点一下。
最佳答案
您可以rename the series 并使用series.to_frame()
df.mean().rename('Mean').to_frame()
或者按照@user3483203的建议,to_frame()
也接受名称参数:
df.mean().to_frame('Mean')
Mean
A 6.000000
B 3.333333
C 8.666667
关于python-3.x - Pandas 添加平均列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63080317/