-1

For the dataframe below,find the top 3 frequent names

Index      Name   
1          Jack     
2          Jack
3          Tom  
4          Tom  
5          Lucy  
6          Lily
7          Lily
The result should be 

  Name      Frequency 
  Jack      2
  Tome      2
  Lily      2

Thanks very much!

Jack
  • 5,540
  • 13
  • 65
  • 113
  • Group by condition with `groupby`, count values with `value_counts`, take top two with `head`. – DYZ May 20 '18 at 05:02

2 Answers2

1

You can try using groupby with apply and nlargest:

result_df = df.groupby('Condition')['Name'].apply(lambda grp: grp.value_counts().nlargest(2)).reset_index()
result_df.columns = ['Condition','Name','Frequency']
print(result_df)

Result:

  Condition  Name  Frequency
0         a  Jack          2
1         a   Tom          2
2         b  Lily          2
3         b  Lucy          1

Update

For edited question, probably following will work:

df.groupby('Name').size().nlargest(3).to_frame('Frequency')

Result:

      Frequency
Name           

Jack          2
Lily          2
Tom           2
niraj
  • 17,498
  • 4
  • 33
  • 48
0

Use this

df.groupby(['Condition','Name']).size()

Here explain better.

Alex Montoya
  • 4,697
  • 1
  • 30
  • 31