Pandas 如何将减法应用于groupby对象

Pandas 如何将减法应用于groupby对象,pandas,Pandas,我有一个这样的数据帧 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

我有一个这样的数据帧

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
我想从新的比率中减去每个类别的旧比率,但不确定如何重新调整DF,以便首先使用,因此可能的减去非常简单:

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