How to get the number of the most frequent value in a column?

前端 未结 6 1794
夕颜
夕颜 2020-12-24 02:02

I have a data frame and I would like to know how many times a given column has the most frequent value.

I try to do it in the following way:

items_co         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 02:45

    You may also consider using scipy's mode function which ignores NaN. A solution using it could look like:

    from scipy.stats import mode
    from numpy import nan
    df = DataFrame({"a": [1,2,2,4,2], "b": [nan, nan, nan, 3, 3]})
    print mode(df)
    

    The output would look like

    (array([[ 2.,  3.]]), array([[ 3.,  2.]]))
    

    meaning that the most common values are 2 for the first columns and 3 for the second, with frequencies 3 and 2 respectively.

提交回复
热议问题