如何将减法应用于groupby对象

时间:2019-05-06 10:25:15

标签: 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          0.20000  old
2   2          0.20000  new
3   2          0.40000  old
4   3          0.40000  new
5   3          0.80000  old

我想从新比例中减去每个类别的旧比例,但是不确定如何重塑DF的形状

2 个答案:

答案 0 :(得分:1)

首先使用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

答案 1 :(得分:0)

另一种方法

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