我正在尝试迭代一列以实现句子中每个单词的计数。
我有一个列:
words |
---|
一二三四四六 |
"…"> |
试试这样做:以pd方式导入pandas
words = ["one two three four four six", "seven eight nine ten eleven", "twelve thirteen fourteen"]
df = pd.DataFrame(words, columns=["word"])
print(df)
wordss = []
for i in df['word']:
wordss += i.split()
wfreq = [wordss.count(w) for w in wordss]
ini_dict = dict(zip(wordss, wfreq))
keys, values = zip(*ini_dict.items())
print("keys : ", str(keys))
print("values : ", str(values))
我已经遍历了整个列,并从每行提取单词。简单!它给了我输出:
word
0 one two three four four six
1 seven eight nine ten eleven
2 twelve thirteen fourteen
keys : ('one', 'two', 'three', 'four', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen')
values : (1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
然后您可以简单地使用Pandas创建DataFrame,如:
ndf = pd.DataFrame(ini_dict, index=["count"])
print(ndf.transpose())
显示如下:
count
one 1
two 1
three 1
four 2
six 1
seven 1
eight 1
nine 1
ten 1
eleven 1
twelve 1
thirteen 1
fourteen 1
enter code here