value_counts 统计每种数据(DataFrame, Series, 1-d ndarray)出现的频次

1.pandas.Series.value_counts

 df = pd.DataFrame(np.arange(9).reshape(3, 3),  columns=list('ABC'))
    print(df)
    '''
       A  B  C
    0  0  1  2
    1  3  4  5
    2  6  7  8
    '''
    c=df['C']
    print(type(c))
    '''
    <class 'pandas.core.series.Series'>
    '''
    print(c)
    '''
    0    2
    1    5
    2    8
    '''
    print(c.value_counts())
    '''
    2    1
    5    1
    8    1
    '''

2.pd.value_counts()

 df = pd.DataFrame(np.arange(9).reshape(3, 3),  columns=list('ABC'))
    print(df)
    ''''
       A  B  C
    0  0  1  2
    1  3  4  5
    2  6  7  8
    '''
    print(df.apply(pd.value_counts))
    '''
         A    B    C
    0  1.0  NaN  NaN
    1  NaN  1.0  NaN
    2  NaN  NaN  1.0
    3  1.0  NaN  NaN
    4  NaN  1.0  NaN
    5  NaN  NaN  1.0
    6  1.0  NaN  NaN
    7  NaN  1.0  NaN
    8  NaN  NaN  1.0
    '''
    # print(pd.value_counts(df))
    '''
    error:  df必须为一维
    '''
    c=df['C']
    print(type(c))
    '''
    <class 'pandas.core.series.Series'>
    '''
    print(pd.value_counts(c))
    '''
    2    1
    5    1
    8    1
    '''
    a=np.array([1,1,1,2,2,2])# 一维数组
    '''
    [1 1 1 2 2 2]
    '''
    print(pd.value_counts(a))
    '''
    2    3
    1    3
    '''
    b = np.array([[1, 1, 1, 2, 2, 2]])  # 二维数组
    print(pd.value_counts(b))
    '''
    error: b为二维数组
    '''

ref:value_counts计算DataFrame,Series的数据频率

pandas.Series.value_counts

pd.value_counts()

0