【发布时间】:2019-08-16 04:55:44
【问题描述】:
我正在尝试计算字典中的值出现在包含词干文本的数据框列中的次数。
我用值列了一个列表,然后应用在计数器函数中计算每行中的每个值
dictionary = {'c-1' : ['x', 'y', 'z'], 'c-2' : ['a', 'b']}
words_list = list()
for key in dictionary.keys():
words_list.append(dictionary[key])
test = [val for sublist in words_list for val in sublist]
from collections import Counter
text['Counter'] = text['Text'].apply(lambda x: Counter([word for word in x if word in test]))
text = {'text': ['some text', some text'], 'Counter': [Counter({a = 1, x = 2}), Counter({b = 2, y = 4, z = 3})]}
我想显示一列,其中包含每行的结果。也许我选择了一种大的方式来做到这一点。我认为这是直接在字典中工作的直接方式,但我不知道具体如何。
【问题讨论】:
-
请提供示例数据和您的预期输出。
-
对于每个字典键,你可以做类似
text['Text'].str.count(r'x|y|z')
标签: python pandas dictionary counter