【发布时间】:2021-12-10 21:36:58
【问题描述】:
我有一个字符串列表和一个带有文本列的数据框。在文本列中,我有几行文本。我想计算字符串列表中每个单词在文本列中出现的次数。我的目标是在数据框中添加两列;一列包含单词,另一列包含出现次数。如果有更好的解决方案,我愿意接受。学习不同的方法来实现这一点会很棒。理想情况下,我希望最后有一个数据框。
string_list = ['had', 'it', 'the']
当前数据框:
代码中的数据框:
pd.DataFrame({'title': {0: 'book1', 1: 'book2', 2: 'book3', 3: 'book4', 4: 'book5'},
'text': {0: 'His voice had never sounded so cold',
1: 'When she arrived home, she noticed that the curtains were closed.',
2: 'He was terrified of small spaces and she knew',
3: "It was time. She'd fought against it for so long",
4: 'As he took in the view from the twentieth floor, the lights went out all over the city'},
'had': {0: 1, 1: 5, 2: 5, 3: 2, 4: 5},
'it': {0: 1, 1: 3, 2: 2, 3: 1, 4: 2},
'the': {0: 1, 1: 4, 2: 5, 3: 3, 4: 3}})
尝试获取这样的数据框:
【问题讨论】:
-
“代码中的数据框”是您想要获取的 DF 吗?
标签: python pandas dataframe text