我需要将df 1的DocN列中的发票付款与df 2的TXT列中的数据进行匹配。根据两个数据集中的匹配文章打印文档(DocN)+金额(DocSum)以及相应付款的详细信息(DocP,Date
import numpy as np
import re
data1 = {
"DocN": ['140111038-001', '7314560', '169233301-001','ЕКТ01886853','ЕКТ02126350','30262-19',
'27283-19','746'],
"DocSum": ['358,80', '1487,45', '7458,78','2478,12','9624,95','3247,32',
'3224,25','32587,22'],
"DocArt1" : ['85647', '85475', '21457', '12746', '25472', '58123', '74185', '82274']
}
df1 = pd.DataFrame(data1)
data2 = {
"TXT": ['payment by document 30262-19, 30317-19, 30329-19, 31270-19, 32038-19, 26713-19,26715-19, ЕКТ01886853 ',
'payment by document 26721-19, 26748-19, 29835-19, 31112-19, 26746-19, 30041-19, 23150-19, ',
'payment by document 23525-19, 25050-19, 26244-19, 27997-19, 28032-19,30278-19, ЕКТ01886853',
'payment by document 29227-19, 29713-19, 27283-19, 32003-19, 29235-19, 29888-19, 7314560',
'payment by document 175634096-001, 175634109-001, 175623281-001,175638863-001, 140111038-001, 7314560'],
"DocP": [112, 113, 114, 115, 116],
"Date": ["25.01.2022", "26.01.2022", "27.01.2022", "28.01.2022", "29.01.2022"],
"DocArt2" : ['12746','74585','25489','85475','85875']
}
df2 = pd.DataFrame(data2)
print(df1)
print(df2)
我正在申请:df1.join(df1.DocN.apply(lambda x: pd.Series(df2.loc[df2['TXT'].str.contains(fr'\b{x}\b')& (df1['DocArt1'] == df2['DocArt2']),['DocP','Date']].to_dict('list'))))
我期待:
| 指数|DocN|DocSum|DocArt1|DocP|日期|
| --------------|--------------|--------------|--------------|--------------|--------------|
| 0|140111038-001|358,80|85647|一百一十六|2022年1月29日|
| 1|七三一四五六零|1487.45|85475|一百一十五|2022年1月28日|
| 二|169233301-001|七四五八,七十八|二一四五七|||
| 三|沪公网安备31010502000112号|2478.12|12746|一百一十二|25.01.2022|
| 四|沪公网安备31010502000112号|9624,95|25472|||
| 五|30262-19|3247,32|五八一二三|一百一十二|25.01.2022|
| 六|27283-19|3224.25|七四一八五|一百一十五|2022年1月28日|
| 七|七四六|32587,22|82274|||
1条答案
按热度按时间mzillmmw1#
第一个解决方案是
Series.str.findall
和DataFrame.explode
,如果需要,则通过DocArt
和DocN
两列进行匹配:如果需要仅通过
DocN
匹配,并通过相同的DocN
聚合:因为否则会得到重复的新行: