【问题标题】:Is there a method to filter in dataframe?有没有在数据框中过滤的方法?
【发布时间】:2021-07-04 06:04:38
【问题描述】:

我有一个单词列表:["sport","technologies", "economie"]

我有一个数据框:

 Text 
 I love sport very much. 
 This is a good technologies

我想创建一个新列,其中包含当前文本中存在的列表中的单词

预期输出:

  Text                    ;  words_match
 I love sport very much. ;  ["sport "]
 This is a good technologies ; ["technologies"]

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    Series.str.findall| 列表中的连接值一起用于正则表达式or

    L = ["sport","technologies", "economie"]
    df['words_match'] = df['Text'].str.findall('|'.join(L))
    print (df)
                               Text     words_match
    0      I love sport very much.          [sport]
    1   This is a good technologies  [technologies]
    

    如果想要不区分大小写的值:

    import re
    L = ["sport","technologies", "economie"]
    df['words_match'] = df['Text'].str.findall('|'.join(L), flags=re.I)
    print (df)
                               Text     words_match
    0      I love Sport very much.          [Sport]
    1   This is a good technologies  [technologies]
    

    【讨论】:

    • 如果我想应用 set() 来获得单词匹配列表中的唯一值?
    • @AlainPulcini - 然后使用df['words_match'] = df['Text'].str.findall('|'.join(L)).apply(set)
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多