Faster way to transform group with mean value in Pandas

前端 未结 2 1067
刺人心
刺人心 2020-12-03 20:00

I have a Pandas dataframe where I am trying to replace the values in each group by the mean of the group. On my machine, the line df[\"signal\"].groupby(g).transform(n

2条回答
  •  独厮守ぢ
    2020-12-03 20:38

    Inspired by Jeff's answer. This is the fastest method on my machine:

    pd.Series(np.repeat(grp.mean().values, grp.count().values))
    

提交回复
热议问题