我的数据框看起来像这样:
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[df.columns[2:]].apply(pd.expanding_mean)
- EdChumdf = df.groupby(['cat1','cat2']).transform(pd.expanding_mean)
。 - Fabio Lamannadf.iloc[:,2:] = df.groupby(['cat1','cat2']).transform(pd.expanding_mean)
,这将保留前两列并覆盖其他列(假设这是您所要求的?)。@Fiabetto可能会想把它写成一个答案,因为这似乎是解决问题的不错方法。 - JohnE