pandas: How to get the most frequent item in pandas series?

后端 未结 4 1942
我寻月下人不归
我寻月下人不归 2020-12-20 18:26

How can I get the most frequent item in a pandas series?

Consider the series s

s = pd.Series(\"1 5 3 3 3 5 2 1 8 10 2 3 3 3         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 18:42

    pandas.factorize and numpy.bincount

    This is very similar to @jezrael's Numpy answer. The difference is the use of factorize and not numpy.unique

    • factorize returns an integer factorization and unique values
    • bincount counts how many of each unique value
    • argmax identifies which bin or factor is the most fequent
    • Use the position of the bin returned from argmax to reference the most frequent value from the array of unique values

    i, r = s.factorize()
    r[np.bincount(i).argmax()]
    
    3
    

提交回复
热议问题