我有一份发给客户的发票清单。然而,有时会发送一张错误的发票,随后又被取消。我的 Pandas Dataframe 看起来像这样,除了更大(约 300 万行)
index | customer | invoice_nr | amount | date
---------------------------------------------------
0 | 1 | 1 | 10 | 01-01-2016
1 | 1 | 1 | -10 | 01-01-2016
2 | 1 | 1 | 11 | 01-01-2016
3 | 1 | 2 | 10 | 02-01-2016
4 | 2 | 3 | 7 | 01-01-2016
5 | 2 | 4 | 12 | 02-01-2016
6 | 2 | 4 | 8 | 02-01-2016
7 | 2 | 4 | -12 | 02-01-2016
8 | 2 | 4 | 4 | 02-01-2016
... | ... | ... | ... | ...
... | ... | ... | ... | ...
现在,我想删除所有行customer
, invoice_nr
and date
是相同的,但是amount
具有相反的值。
发票更正始终在同一天进行,发票编号相同。发票号码与客户唯一绑定,并且始终对应于一笔交易(可以由多个部分组成,例如customer = 2
, invoice_nr = 4
)。发票更正仅发生在更改amount
收费,或分裂amount
在较小的组件中。因此,取消的值不会在相同的值上重复invoice_nr
.
任何有关如何编程的帮助将不胜感激。
def remove_cancelled_transactions(df):
trans_neg = df.amount < 0
return df.loc[~(trans_neg | trans_neg.shift(-1))]
groups = [df.customer, df.invoice_nr, df.date, df.amount.abs()]
df.groupby(groups, as_index=False, group_keys=False) \
.apply(remove_cancelled_transactions)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)