【发布时间】:2018-01-23 06:48:00
【问题描述】:
我有一个包含 3 列的 pandas 数据框:key1, key2, document。所有三列都是文本字段,大小为document,范围从50 个字符到5000 个字符。我根据每个(key1, key2) 的文档集的最小频率确定了一个词汇表,我为其使用scikit-learn CountVectorizer 并设置min_df。我可以使用df.groupby[['key1','key2']]['document'].apply(vocab).reset_index() 来做到这一点,其中vocab 是一个函数,我在其中计算并返回作为集合的词汇表(如上定义)。
现在,我想使用这些词汇表(每个key1, key2 一组)来过滤相应的文档,以便每个文档只包含其词汇表中的单词。对于这部分我能得到任何帮助,我将不胜感激。
样本数据
Input
key1 | key2 | document
aa | bb | He went home that evening. Then he had soup for dinner.
aa | bb | We want to sit down and eat dinner
cc | mm | Sometimes people eat in a restaurant
aa | bb | The culinary skills of that chef are terrible. Let us not go there.
cc | mm | People go home after dinner and try to sleep.
Vocabulary - not using counts for the purpose of this example
key1 | key2 | vocab
aa | bb | {went, evening, sit, down, culinary, chef, dinner}
cc | mm | {people, restaurant, home, dinner, sleep}
Result - only use words from corresponding vocab in document
key1 | key2 | document
aa | bb | went evening dinner
aa | bb | sit down dinner
cc | mm | people restaurant
aa | bb | culinary chef
cc | mm | people home dinner sleep
【问题讨论】:
-
您可以添加一些数据样本 - 5 行的预期输出吗?
-
添加了示例数据。谢谢!
标签: python pandas pandas-groupby