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

前端 未结 6 1833
夕颜
夕颜 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:40

    Just take the first row of your items_counts series:

    top = items_counts.head(1)  # or items_counts.iloc[[0]]
    value, count = top.index[0], top.iat[0]
    

    This works because pd.Series.value_counts has sort=True by default and so is already ordered by counts, highest count first. Extracting a value from an index by location has O(1) complexity, while pd.Series.idxmax has O(n) complexity where n is the number of categories.

    Specifying sort=False is still possible and then idxmax is recommended:

    items_counts = df['item'].value_counts(sort=False)
    top = items_counts.loc[[items_counts.idxmax()]]
    value, count = top.index[0], top.iat[0]
    

    Notice in this case you don't need to call max and idxmax separately, just extract the index via idxmax and feed to the loc label-based indexer.

提交回复
热议问题