gpt4 book ai didi

python - 计算 df pandas 中所有列的扩展平均值

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:59 25 4
gpt4 key购买 nike

我的 df 看起来像这样:

    cat1     cat2     x1     x2     x3      x4      x5      x6   . . .
0 str str float float float float float float . . .
1 str str float float float float float float . . .
. . . . . . . . . . . .
. . . . . . . . . . . .

我已经试过了:

df = df.groupby(['cat1','cat2']).apply(pd.expanding_mean)

但这给了我一个

ValueError: could not convert string to float: 

唯一的字符串在 groupby 中。这工作正常,但不是我需要的:

df = df.groupby(['cat1','cat2']).mean()

最佳答案

也感谢其他用户的努力,以下可能是一个解决方案:

df.iloc[:,2:] = df.groupby(['cat1','cat2']).transform(pd.expanding_mean)

保留前两列。

关于python - 计算 df pandas 中所有列的扩展平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525600/

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