0

Given a DataFrame like this:

BeginningDataframe

Desired outcome:

DesiredOutput

There are solutions that are close to what I'm looking for using groupby and count, but I'm not sure how to get it right. Basically, I want the recommendation that has the highest count for each symbol to be returned. So I assume pseudo code might look something like:

new_df = df.groupby(['Symbol'])['Recommendation'].count().max()

...but of course this is wrong.

wildcat89
  • 1,159
  • 16
  • 47

1 Answers1

3

You could apply mode:

new_df = df.groupby('Symbol')['Recommendation'].apply(lambda x: x.mode())

I guess we could also do the following:

s = df.groupby(['Symbol','Recommendation']).size()
s = s.groupby(level=0).transform('max').eq(s)
out = s.index[s].to_frame().reset_index(drop=True)