[英]calculating expanding mean for all columns in df pandas
My df looks like this: 我的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 . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
I've tried this: 我试过这个:
df = df.groupby(['cat1','cat2']).apply(pd.expanding_mean)
but that gives me a 但这给了我一个
ValueError: could not convert string to float:
The only strings are in the groupby. 唯一的字符串在groupby中。 This works fine but isn't what I need: 这很好但不是我需要的:
df = df.groupby(['cat1','cat2']).mean()
Thanks also to other users' efforts, the following might be a solution: 还要感谢其他用户的努力,以下可能是一个解决方案:
df.iloc[:,2:] = df.groupby(['cat1','cat2']).transform(pd.expanding_mean)
Which preserves the first two columns. 这保留了前两列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.