早上好,
我有以下数据集:
print(df)
Date Type of Operation Quantity
01/01/17 Order 5
01/01/17 Sale 3
02/01/17 Sale 1
02/01/17 Order 5
我想创建以下列:
print(df_1)
Date Type of Operation Quantity Actual_Inventory
01/01/17 Order 5 5
01/01/17 Sale 3 2
02/01/17 Sale 1 1
02/01/17 Order 5 6
有关如何使用Python和pandas对其进行编码的任何建议? 谢谢
答案 0 :(得分:0)
如果Quantity
,您可以在-1
之前添加多列Sale
,然后使用cumsum
:
m = df['Type of Operation'] == 'Sale'
df['Actual_Inventory'] = df['Quantity'].mask(m, df['Quantity'] * -1).cumsum()
另一种解决方案:
a = (df['Type of Operation'] == 'Sale').map({True:-1, False:1})
df['Actual_Inventory'] = df['Quantity'].mul(a).cumsum()
print (df)
Date Type of Operation Quantity Actual_Inventory
0 01/01/17 Order 5 5
1 01/01/17 Sale 3 2
2 02/01/17 Sale 1 1
3 02/01/17 Order 5 6