1

This is directly based off of the question GroupBy pandas DataFrame and select most common value. My goal is to groupby one column, and keep the value that occurs most often in a second column. The solution I am using is df.groupby(['col1'])['col2'].agg(lambda x: x.value_counts().index[0]) I would like to expand on this solution to drop any entries where there is a tie.

keenan
  • 462
  • 3
  • 12
  • Does this answer your question? [GroupBy pandas DataFrame and select most common value](https://stackoverflow.com/questions/15222754/groupby-pandas-dataframe-and-select-most-common-value) – Pravash Panigrahi Apr 30 '23 at 02:38
  • @pravashpanigrahi as stated in my question, I would like to expand on the answer provided by that post to drop situations where there is a tie. So no, it does not. – keenan May 01 '23 at 18:33

1 Answers1

1

You can use pd.Series.mode to get all the modes, then filter:

# sample data
df = pd.DataFrame({'group': list('aaaabbbb'), 'value': [1,1,1,2,1,1,2,2] })

# output
out = (df.groupby('group')['value'].apply(pd.Series.mode)
         .groupby('group').filter(lambda x: len(x)==1)
      )

Output:

group   
a      0    1
Name: value, dtype: int64
​
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74