我有一個問題,我想計算資料框中的唯一單詞,但不幸的是它只計算第一句話。
text
0 hello is a unique sentences
1 hello this is a test
2 does this works
import pandas as pd
d = {
"text": ["hello is a unique sentences",
"hello this is a test",
"does this works"],
}
df = pd.DataFrame(data=d)
from collections import Counter
# Count unique words
def counter_word(text_col):
print(len(text_col.values))
count = Counter()
for i, text in enumerate(text_col.values):
print(i)
for word in text.split():
count[word] = 1
return count
counter = counter_word(df['text'])
len(counter)
uj5u.com熱心網友回復:
我認為更簡單的是按空格連接值,然后拆分單詞并計數:
counter = Counter((' '.join(df['text'])).split())
print (counter)
Counter({'hello': 2, 'is': 2, 'a': 2, 'this': 2, 'unique': 1, 'sentences': 1, 'test': 1, 'does': 1, 'works': 1})
uj5u.com熱心網友回復:
您可以使用itertools.chain生成器來饋送Counter:
from itertools import chain
counter = Counter(chain.from_iterable(map(str.split, df['text'])))
輸出:
Counter({'hello': 2,
'is': 2,
'a': 2,
'unique': 1,
'sentences': 1,
'this': 2,
'test': 1,
'does': 1,
'works': 1})
uj5u.com熱心網友回復:
stack將單詞放入單個列然后用于pandas value_counts計算它們可能更容易和更有效,而不是Counter:
df["text"].str.split(expand=True).stack().value_counts()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/493218.html
