Search code examples
pandasdataframemodeany

How to get the mode of a column in pandas where there are few of the same mode values pandas


I have a data frame and i'd like to get the mode of a specific column. i'm using:

freq_mode = df.mode()['my_col'][0]

However I get the error:

ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index my_col')

I'm guessing it's because I have few mode that are the same. I need any of the mode, it doesn't matter. How can I use any() to get any of the mode existed?


Solution

  • For me your code working nice with sample data.

    If necessary select first value of Series from mode use:

    freq_mode = df['my_col'].mode().iat[0]