gpt4 book ai didi

python - Pandas :从列 A 中提取 B 列不存在的数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:37 25 4
gpt4 key购买 nike

我的数据包括发票,我必须检查发票是否已经支付。对于每张发票,我循环遍历所有报告日期。如果有一天,发票没有出现,这意味着客户已经付款,当然不会在以后的日子里再次出现。

您可以从下表中看到发票 C 已于 28/05 支付。

Report Date  Invoice No
2019-05-28 D
2019-05-28 A
2019-05-28 B

2019-05-27 A
2019-05-27 B
2019-05-27 C

2019-05-26 A
2019-05-26 B
2019-05-26 C

我在下面编写了代码,它可以工作,但花费的时间太长,因为大约有 80 万个条目。这是非常低效的。我想知道是否有更有效的方法使用 pandas 来解决这个问题。

# For every Invoice
for i in range(0,len(documentNo.categories)):
# If the invoice still exists in the newest Report Date (here 28/05), means that it has not been paid yet. So we can skip to check other invoices
if (df.loc[(df['Document No'] == documentNo.categories[i]) & (df['Report Date'] == reportDates.categories[len(reportDates.categories) - 1])].all(1).any()):
continue

# Decrement from date 27/05
for j in range(len(reportDates.categories) - 2,0,-1):

# If the Invoice does not exist on this date, it has been paid
if (df.loc[(df['Document No'] == documentNo.categories[i]) & (df['Report Date'] != reportDates.categories[j])].all(1).any()):
break

因此,我想要一个新列来显示每一行的打开/关闭。

Report Date  Invoice No  Open/Closed
2019-05-28 D Open
2019-05-28 A Open
2019-05-28 B Open

2019-05-27 A Open
2019-05-27 B Open
2019-05-27 C Closed

2019-05-26 A Open
2019-05-26 B Open
2019-05-26 C Closed

最佳答案

这里是我们使用crosstab的方法,然后是 invoice eq 到 0 ,这意味着以前的发票应该算作 Closed

s=pd.crosstab(df.ReportDate,df.InvoiceNo).eq(0)

Newdf=(s.iloc[::-1,:].cummax()&~s).replace({True:'Closed',False:'Open'}).stack().reindex(pd.MultiIndex.from_frame(df)).reset_index()
Newdf
Out[342]:
ReportDate InvoiceNo 0
0 2019-05-28 D Open
1 2019-05-28 A Open
2 2019-05-28 B Open
3 2019-05-27 A Open
4 2019-05-27 B Open
5 2019-05-27 C Closed
6 2019-05-26 A Open
7 2019-05-26 B Open
8 2019-05-26 C Closed

关于python - Pandas :从列 A 中提取 B 列不存在的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com