My df looks like this:
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.