Inplace transformation pandas with groupby

后端 未结 2 1013
孤街浪徒
孤街浪徒 2020-12-21 08:20

Would it be possible to mutate DataFrame inplace with groupby statement?

import pandas as pd
dt = pd.DataFrame({
                       


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 08:46

    I think you can use transform which return Series same length and same index as df with substracting:

    print (dt.groupby("LETTER")['VALUE'].transform('mean'))
    0     5.0
    1    13.5
    2    13.0
    3     5.0
    4    13.5
    Name: VALUE, dtype: float64
    
    dt['NEW_COL'] = dt['VALUE'] - dt.groupby("LETTER")['VALUE'].transform('mean')
    print (dt)
      LETTER  VALUE  NEW_COL
    0      a     10      5.0
    1      b     12     -1.5
    2      c     13      0.0
    3      a      0     -5.0
    4      b     15      1.5
    

提交回复
热议问题