gpt4 book ai didi

python-3.x - Pandas 添加平均列标题

转载 作者:行者123 更新时间:2023-12-02 02:44:08 25 4
gpt4 key购买 nike

我正在尝试为平均列添加列标题。引用下文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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com