Python 从数据帧中的行中减去双计数值

Python 从数据帧中的行中减去双计数值,python,pandas,Python,Pandas,我正在与Pandas合作,有一个如下所示的数据框: Module Position Layout Count x a Desktop 50 x a Mobile 20 y a Desktop 100 y a Mobile 30 z b Desktop 80 z b Mobile 20 当模

我正在与Pandas合作,有一个如下所示的数据框:

Module  Position    Layout  Count
x       a           Desktop 50
x       a           Mobile  20
y       a           Desktop 100
y       a           Mobile  30
z       b           Desktop 80
z       b           Mobile  20
当模块和位置值匹配时,如何从桌面计数中减去移动计数以获得结果数据帧:

Module  Position    Layout  Count
x       a           Desktop 30
x       a           Mobile  20
y       a           Desktop 70
y       a           Mobile  30
z       b           Desktop 60
z       b           Mobile  20

如果我在原始数据帧上有一个数字索引,有没有办法维护这些值,而不是让每一列都是非索引的数字?
d1 = df.set_index(['Module', 'Position', 'Layout'])
d2 = d1.unstack().diff(-1, axis=1).stack().combine_first(d1).reset_index()
print(d2)

  Module Position   Layout  Count
0      x        a  Desktop   30.0
1      x        a   Mobile   20.0
2      y        a  Desktop   70.0
3      y        a   Mobile   30.0
4      z        b  Desktop   60.0
5      z        b   Mobile   20.0