我的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)
这保留了前两列.