首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中列中的模式匹配

python中列中的模式匹配
EN

Stack Overflow用户
提问于 2018-07-25 10:16:34
回答 1查看 415关注 0票数 1

我有两个数据帧df和df1。我想根据df1中给出的值在df中搜索模式。DataFrames如下所示:

代码语言:javascript
复制
    import pandas as pd
    data={"id":["I983","I873","I526","I721","I536","I327","I626","I213","I625","I524"],
"coltext":[ "I could take my comment back, I would do so in a second. I have addressed my teammates and coaches and while many understand my actions were totall",                                                                                                "We’re just trying to see if he can get on the field as a football player, and then we’ll make decision",
                                                                                                 "TextNow offers low-cost, international calling to over 230 countries. Stay connected longer with rates starting at less than",
                                                                                                 "Wi-Fi can provide you with added coverage in places where cell networks don't always work - like basements and apartments. No roaming fees for Wi-Fi connection",
                                                                                                 "Send messages and make calls on your compute",
                                                                                                 "even have a free, Wi-Fi only version of TextNow, available for download on you",
                                                                                                 "the rest of the players accepted apologies this spring and are welcoming him back",
                                                                                                 "was really looking at him and watching how much this really means to him and how much he really missed us",
                                                                                                 "I’ll deal with the problem and I’ll remedy the problem",
                                                                                                 "The first step was for him to be able to complete what we call our bottom line program which has been completed"]}
df=pd.DataFrame(data=data)
data1={"col1":["addressed teammates coaches","football player decision","watching really missed", "bottom line program","meassges make calls"],
     "col2":["international calling over","download on you","rest players accepted","deal problem remedy","understand actions totall"],
     "col3":["first step him","Wi-Fi only version","cell network works","accepted apologies","stay connected longer"]}
df1=pd.DataFrame(data=data1)

例如,df1'col1‘中的第一个元素"addressed teammates“位于df’‘coltext’中的第一个元素中,同样,我希望在df‘’coltext‘中搜索df1中每一列中的每个元素。如果找到了模式,则在df中创建第三列。

所需输出:

代码语言:javascript
复制
id  coltext                                 patternMatch
I983  I could take my comment back,               col1, col2
I873  We’re just trying to see if he can              col1
I526  TextNow offers low-cost,                    col3, col2
I721  Wi-Fi can provide you with                      col3
I536  Send messages and make calls                    col1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-25 10:59:32

可能还有其他有效的方法,其中一种方法可能如下:

代码语言:javascript
复制
# create dictionary of data1 such that values and keys are reversed
my_dict = {item:k for k, v in data1.items() for item in v}
# for column in df check if all words are in 'coltext' for each key in dictionary
df['patternMatch'] = df['coltext'].apply(lambda row: 
                                         {v for k, v in my_dict.items() 
                                                if all(word in row for word in k.split())})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51509915

复制
相关文章

相似问题

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