pandas Series.value_counts returns inconsistent order for equal count strings

后端 未结 3 2042
野性不改
野性不改 2021-01-12 14:38

When I run the code below:

s = pandas.Series([\'c\', \'a\', \'b\', \'a\', \'b\'])
print(s.value_counts())

Sometimes I get this:

<         


        
3条回答
  •  半阙折子戏
    2021-01-12 15:13

    Adding a reindex after value_counts

    df.value_counts().reindex(df.unique())
    Out[353]: 
    a    1
    b    1
    dtype: int64
    

    Update

    s.value_counts().sort_index().sort_values()
    

提交回复
热议问题