如何将减法应用于groupby对象



我有一个像这样的数据框架

test = pd.DataFrame({'category':[1,1,2,2,3,3],
                     'type':['new', 'old','new', 'old','new', 'old'], 
                     'ratio':[0.1,0.2,0.2,0.4,0.4,0.8]})
    category    ratio   type
0   1          0.10000  new
1   1          0.20000  old
2   2          0.20000  new
3   2          0.40000  old
4   3          0.40000  new
5   3          0.80000  old

我想从新比率中减去每个类别的旧比例

首先使用 DataFrame.pivot,因此可能会减去非常简单:

df = test.pivot('category','type','ratio')
df['val'] = df['old'] - df['new']
print (df)
type      new  old  val
category               
1         0.1  0.2  0.1
2         0.2  0.4  0.2
3         0.4  0.8  0.4

另一种方法

df =  df.groupby('category').apply(lambda x: x[x['type'] == 'old'].reset_index()['ratio'][0] - x[x['type'] == 'new'].reset_index()['ratio'][0]).reset_index(name='val')

输出

  category  val
0         1  0.1
1         2  0.2
2         3  0.4

最新更新