使用groupby转换 pandas [英] Inplace transformation pandas with groupby

查看:100
本文介绍了使用groupby转换 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用groupby语句对DataFrame进行就地突变?

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

import pandas as pd
dt = pd.DataFrame({
                   "LETTER": ["a", "b", "c", "a", "b"],
                   "VALUE" : [10 , 12 , 13,  0,  15]
                   })
def __add_new_col(dt_):
    dt_['NEW_COL'] = dt_['VALUE'] - dt_['VALUE'].mean()
    return dt_
pass


dt.groupby("LETTER").apply(__add_new_col)
  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


dt
  LETTER  VALUE
0      a     10
1      b     12
2      c     13
3      a      0
4      b     15

在R data.table中,可以使用:=运算符,例如dt[, col := ... , by ='LETTER']

In R data.table it is possible by using := operator e.g. dt[, col := ... , by ='LETTER']

推荐答案

我认为您可以使用

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

这篇关于使用groupby转换 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆