value_counts()函数
value_counts函数用于统计dataframe或series中不同数或字符串出现的次数
ascending=True时,按升序排列.
normalize=True时,可计算出不同字符出现的频率,画柱状图统计时可以用到.
# trian中标签的比例 label_proportion = train['label'].value_counts(normalize=True).reset_index().sort_values(by=['index']) # index label # 5 1 0.029851 # 2 2 0.199005 # 0 3 0.298507 # 1 4 0.248756 # 3 5 0.149254 # 4 6 0.074627
df1= DataFrame( {"a":[3,4,5,6,2,3,4,4], "b":[2,4,5,6,5,4,3,4]} ) print(df1) #dataframe要借助apply来应用value_counts() print(df1.apply(pd.value_counts)) # map中括号内是series类型,key是a列的数,values是出现的次数 print(df1['a'].map(df1['a'].value_counts()))
print(df1['a'].value_counts()) #加括号时可直接统计出a列每个元素出现的次数
a b 0 3 2 1 4 4 2 5 5 3 6 6 4 2 5 5 3 4 6 4 3 7 4 4 a b 2 1 1 3 2 1 4 3 3 5 1 2 6 1 1 0 2 1 3 2 1 3 1 4 1 5 2 6 3 7 3 Name: a, dtype: int64
https://blog.csdn.net/qq_20412595/article/details/79921849
https://blog.csdn.net/qq_42665335/article/details/81177699