标签:
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 defaultdict def get_counts(sequence): counts = defaultdict(int)#所有的值均会被初始化为0 for x in sequence: counts[x] +=1 return counts
from collections import Counter counts = Counter(‘列表名‘) counts. most_common(10)#前10位的计数
对于DataFrame对象中的一列所返回的对象Series,例如frame[‘属性名‘],有一个value_counts方法,
counts = frame[‘属性名‘].value_counts()
标签:
原文地址:http://www.cnblogs.com/Yiutto/p/5685352.html