【问题标题】:Check words from a list in column of dataframe and print the words in another created column of the same dataframe [Python]从数据框列中的列表中检查单词并在同一数据框的另一个创建列中打印单词 [Python]
【发布时间】:2021-06-21 07:27:48
【问题描述】:

我有一个关键字列表,我希望编写一个 Python 程序,它可以迭代列表中的每个单词并检查列表中的单词是否存在于数据框列的每一行中,并将这些单词打印在另一列中相同的数据框。

例如

keywords = ['registration', 'al', 'branch']
df = pd.DataFrame({'message': ['wonderful registration process', 'i hate this branch', 'this branch has a great registration process','I don't like this place']})

我想用数据框中消息的每一行检查列表中匹配的单词,并在数据框的另一个名为“keywords”的创建列中打印匹配的单词。 所以上面代码的输出应该是

df    
    message
0   wonderful registration process
1   i hate this branch
2   this branch has a great registration process
3   I don't like this place

df    
    message                                               keywords
0   wonderful registration process                        registration
1   i hate this branch                                    branch
2   this branch has a great registration process          registration, branch
3   I don't like this place                               none

如果有人能指导我,那就太好了。

【问题讨论】:

    标签: python list dataframe


    【解决方案1】:

    这是你的解决方案,就像一个魅力。

    import pandas as pd
    keywords = ['registration', 'al', 'branch'] 
    df = pd.DataFrame({'message': ["wonderful registration process", "i hate this branch", "this branch has a great registration process","I don't like this place"]})
    # first of all when you have word like don't try to use ("") not ('') when defining string
    
    #(keyword if (keyword in element)
    def operation(element):
     res=",".join([(keyword) for keyword in keywords if (keyword in element)]) 
     if res=="":
      return "none" #handling no keyword situation
     else:
      return res   
    
    df.insert(1, "keywords", list(map(operation,list(df.to_dict()['message'].values()))), True)#insert of new array
    print(df)
     
    

    编码愉快,有任何问题都可以在 stackoverflow 上给我发短信。

    【讨论】:

    • 太棒了!感谢您的指导!
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2021-12-02
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多