繁体   English   中英

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

[英]calculating expanding mean for all columns in df pandas

我的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)

这保留了前两列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM