[英]How to count how many times a word appears in a column, python
如何計算一個詞在Python的某個列中出現在字符串中的次數? 例如:
file|context
----|-------
1 | Hello world
2 | Round and round
我想計算單詞的出現:
file| context | word_count
----|-----------------|---------------------
1 | Hello world | {'hello':1,'world':1}
2 | Round and round | {'round':2,'and':1}
我整天都在堅持,並嘗試使用value_counts()和Counter。 仍然無法弄清楚。 有什么幫助嗎?
謝謝!
您可以在小寫版本的拆分字符串上使用collections.Counter
:
from collections import Counter
s = 'Round and round'
counts = Counter(s.lower().split())
print(dict(counts))
輸出:
{'and': 1, 'round': 2}
接下來,您需要對此進行調整以使用您的數據。 數據格式似乎使用固定寬度的字段,因此上下文列從位置7開始。假設數據來自文件:
with open('data') as f:
next(f) # skip the header
next(f) # skip the border
# print new header and border
for line in f:
counts = Counter(line[6:].lower().split())
print('{} | {}'.format(line, dict(counts)))
需要做一些工作以將計數正確格式化為輸出列。
下一個給出單詞出現在字符串中的次數計數
str = "Round and round"
dict1={}
for eachStr in str.split():
if eachStr.lower() in dict1.keys():
count = dict1[eachStr]
count = count + 1
dict1[eachStr.lower()] = count
else:
dict1[eachStr.lower()] = 1
print dict1
輸出:
{'and': 1, 'round': 2}
您可以為此使用python內置函數Counter
。
In [5]: from collections import Counter
In [6]: string = 'Hello world'
In [9]: count = Counter(string.lower().split())
In [10]: print(dict(count))
{'world': 1, 'hello': 1}
將字詞轉換為lowercase
是因為Counter
對大寫字母和小寫字母的考慮不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.