我有一个使用pandas()创建的数据透视表,如下所示:
Type Type-1 Type-2 Type-3 Grand Total
product
A 5 0 0 5
B 0 6 9 15
C 0 0 0 0
D 12 7 44 63
E 6 0 0 6
Deduction 0 0 9 9
H 50 6 8 64
I 0 0 0 0
Grand Total 73 19 70 162现在,我想创建一个名为"W/O Deduction“的第二个边距,它基本上是从相应的列中减去扣除值。所以最终的结果应该是这样的:
Type Type-1 Type-2 Type-3 Grand Total
product
A 5 0 0 5
B 0 6 9 15
C 0 0 0 0
D 12 7 44 63
E 6 0 0 6
Deduction 0 0 9 9
H 50 6 8 64
I 0 0 0 0
Grand Total 73 19 70 162
With DEDUCTION 73 19 61 153我如何使用panda来实现这一点?
发布于 2020-01-28 21:58:00
用fill_value=0参数将NaNs替换为0 in counts时,使用Series.sub和fill_value=0参数将所有列的减值填充为新行:
df.loc['W/O DEDUCTION'] = df.loc['Grand Total'].sub(df.loc['Deduction'], fill_value=0)
#alternative
#df.loc['W/O DEDUCTION'] = df.loc['Grand Total'] - df.loc['Deduction']
print (df)
Type-1 Type-2 Type-3 Grand Total
Type
A 5 0 0 5
B 0 6 9 15
C 0 0 0 0
D 12 7 44 63
E 6 0 0 6
Deduction 0 0 9 9
H 50 6 8 64
I 0 0 0 0
Grand Total 73 19 70 162
W/O DEDUCTION 73 19 61 153https://stackoverflow.com/questions/59950320
复制相似问题