【发布时间】:2020-05-16 04:24:56
【问题描述】:
我有两个列表,我正在尝试查找另一个列表中包含的一个列表中的字数
样本数据
list1 = ['apple','orange','ball','peach']
df['list2'] = ['Apples were served as the dessert','They like apples','I prefer oranges to apples.','Tom drank his orange juice','These oranges have gone bad','He could hit the ball, too']
我的输出应该是
apples 2
oranges 3
ball 1
我可以通过提取每个字符串来计数,但我的 list1 有 60 多个单词。如果它包含在list2中,是否可以获得所有单词的计数?提前致谢
我的代码
df = df[df.list2.str.lower().str.contains('apples')]
【问题讨论】:
-
“我的代码”不是有效的 Python。
-
@ScottHunter - 这是有效的熊猫代码
-
@jezrael 至少需要在问题中提及熊猫。
-
这个也需要模糊匹配