Python中的計數(詞頻)

來源:互聯網
上載者:User

標籤:

1,對於list列表來說a.用自訂函數來統計技術
def get_count(sequence):    counts={}    for x in sequence:        if x in sequence:            count[x] +=1        else:            count[x] =1    return counts

或者利用python標準庫

from collections import defaultdictdef get_counts(sequence):        counts = defaultdict(int)#所有的值均會被初始化為0    for x in sequence:        counts[x] +=1    return counts
b.利用python標準庫的collections.Counter類
from collections import Countercounts = Counter(‘列表名‘)counts. most_common(10)#前10位的計數
2,對於DataFrame來說

對於DataFrame對象中的一列所返回的對象Series,例如frame[‘屬性名稱‘],有一個value_counts方法,

counts = frame[‘屬性名稱‘].value_counts()

Python中的計數(詞頻)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.