如果您提供某种可运行的示例,它会有所帮助:
df = pd.DataFrame(dict(
id = ['a', 'b', 'c', 'd'],
objeto = ['Foo bar', 'hello Hi FOO', 'Yes hi Hello', 'Pythons PaNdas yeS']
))
stop_words = ['foo', 'bar']
这里的主要问题不是用熊猫来计数。
熊猫有
.value_counts()
在这种情况下,您希望将所有单词放在一列中,您可以使用
.explode()
df['objeto'].str.casefold().str.split().explode()
0 foo
0 bar
1 hello
1 hi
1 foo
2 yes
2 hi
2 hello
3 pythons
3 pandas
3 yes
Name: objeto, dtype: object
你可以
.mask()
删除以下单词
.isin(stop_words)
然后
.value_counts()
df['objeto'].str.casefold().str.split().explode().mask(lambda word: word.isin(stop_words)).value_counts()
objeto
hello 2
hi 2
yes 2
pythons 1
pandas 1
Name: count, dtype: int64