【问题标题】:How do I get strings that match substrings in a pandas df column in another column?如何获取与另一列中 pandas df 列中的子字符串匹配的字符串?
【发布时间】:2019-08-30 06:11:30
【问题描述】:

我有一个字符串列表、技能列表和一个 pandas 数据框,在标有“工作摘要”的列下的每一行中都有描述。我想查看 Skills 中的任何字符串是否是“Job Summary”列中的子字符串。如果有匹配项,则匹配的字符串出现在标记为 Matches 的列中。如果有多个,那么它应该显示为字符串列表。现在我有它,它可以告诉我是非,但我希望单词本身匹配。

看看我目前在下面有什么

     #Sample list (Real list is much longer)
     Skills=['Science', 'Management','Equipment','Analysis']
     skills=list(map(str.lower,skills))

     joined='|'.join(skills)

     df['Matches']=df['Job Summary'].str.contains(joined)

df['Matches'] 的结果告诉我是真是假。我想要匹配的单词

【问题讨论】:

    标签: python pandas list substring


    【解决方案1】:

    使用str.findall

    df=pd.DataFrame({'Job Summary':['Science Equipment','Analysis is Management']})
    df['Job Summary'].str.findall('|'.join(Skills))
    Out[95]: 
    0      [Science, Equipment]
    1    [Analysis, Management]
    Name: Job Summary, dtype: object
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2020-09-20
      • 2018-08-28
      • 2023-03-28
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多