【发布时间】: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
如果有人能指导我,那就太好了。
【问题讨论】: