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 defaultdict def get_counts(sequence): counts = defaultdict(int)#所有的值均会被初始化为0 for x in sequence: counts[x] +=1 return counts
b.利用python标准库的collections.Counter类
from collections import Counter counts = Counter(‘列表名‘) counts. most_common(10)#前10位的计数
2,对于DataFrame来说
对于DataFrame对象中的一列所返回的对象Series,例如frame[‘属性名‘],有一个value_counts方法,
counts = frame[‘属性名‘].value_counts()
时间: 2024-10-04 10:41:50