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

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

    The NaN values are omitted for calculating frequencies. Please check your code functionality here But you can use the below code for same functionality.

    **>> Code:**
        # Importing required module
        from collections import Counter
    
        # Creating a dataframe
        df = pd.DataFrame({ 'A':["jan","jan","jan","mar","mar","feb","jan","dec",
                                 "mar","jan","dec"]  }) 
        # Creating a counter object
        count = Counter(df['A'])
        # Calling a method of Counter object(count)
        count.most_common(3)
    
    **>> Output:**
    
        [('jan', 5), ('mar', 3), ('dec', 2)]
    

提交回复
热议问题