来自不同列的Python Pandas字符串匹配 - python

我有一个excel-1(原始数据)和excel-2(参考文档)

在excel-1中,“ Comments”应与excel-2“ Comments”列相匹配。如果excel-1“ comments”列中的字符串包含excel-2“ comments”列中的任何子字符串,则主要原因和次要原因excel-1中的每一行都应填充excel-2中的原因。

Excel-1
{'Item':{0:'rr-1',1:'ss-2'},'Order':{0:1,1:2},'Comments':{0:'Good;缺货订单,#1237-MF,关闭库存',1:'不变,不好,下周发货,09/12 / 2018-MF *'}}

Excel-2
{“评论”:{0:“良好”,1:“库存无序”,2:“库存已关闭”,3:“无变化”,4:“库存不足”,5:“下周交货”} ,“主要原因”:{0:“质量”,1:“仓库”,2:“物流”,3:“反馈”,4:“仓库”,5:“物流”},“次要原因”:{ 0:“制造”,1:“库存”,2:“仓库”,3:“反馈”,4:“库存”,5:“仓库”}}

请帮助建立逻辑。

当使用pd.dataframe.str.contains / isin函数进行单个匹配时,我得到了答案,但是如何编写逻辑来搜索多个匹配并以特定的结构格式编写。

来自不同列的Python Pandas字符串匹配 - python

for value in df['Comments']:
    string = re.sub(r'[?|$|.|!|,|;]',r'',value)
    for index,value in df1.iterrows():
        substring = df1.Comment[index]
        if substring in string:
            df['Primary Reason']= df1['Primary Reason'][index]
            df['Secondary Reason']=df1['Secondary Reason'][index]

参考方案

对于df ['Comments']中的值:

string = re.sub(r'[?|$|.|!|,|;]',r'',value)

for index,value in df1.iterrows():

    substring = df1.Comment[index]

    if substring in string:

        df['Primary Reason']= df1['Primary Reason'][index]

        df['Secondary Reason']=df1['Secondary Reason'][index]

从上面的代码分析:

基本上,您正在比较excel-1的row1和excel-2的row-1并匹配子字符串和字符串,并获得主要和次要原因正确吗?
在这里,您覆盖了相同的位置,即o / p位置,因此,您总是只能得到1个结果。

问题在以下代码中:

df['Primary Reason']= df1['Primary Reason'][index]

df['Secondary Reason']=df1['Secondary Reason'][index]

提出逻辑,您可以按照以下格式在同一行中累加结果

res1,res2 .... etc

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…