首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自不同列的Python Pandas字符串匹配

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

Stack Overflow用户
提问于 2018-09-20 21:54:06
回答 1查看 519关注 0票数 1

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

在excel-1中,"Comments“应与excel-2 \f25 "Comments”-2\f6中的"comments“-2\f6进行匹配。-1\f25 excel-1 \f25 "comments”-1\f6列中的字符串包含-2\f25 excel-2 \f25“comments”-2\f6列中的任意子字符串,excel-2 \f6中的-2\f25 matched -2\f6和-2\f25 the -2\f6应在-2\f25 excel-1 \f6中的每一行中填写。-2\f25 column.If -2\f6。

Excel-1 {'Item':{0:'rr-1',1:'ss-2'},' order ':{0: 1,1: 2},'Comments':{0:‘好;库存无序,#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.concludes/isin函数得到了答案,但是如何编写逻辑来搜索多个匹配项,并以特定的结构格式编写。

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

发布于 2018-09-21 04:14:30

对于df‘’Comments‘中的值

代码语言:javascript
复制
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]

以上代码的分析:

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

问题出现在以下代码中:

代码语言:javascript
复制
df['Primary Reason']= df1['Primary Reason'][index]

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

  1. 提出了这样的逻辑,您可以将结果相加,结果的行格式如下所示

res1,res2 ....etc

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52426807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档