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