nick
nick

Reputation: 149

get nlargest distinct values in pandas

I am trying to get top 3 distinct values from a df, nlargest wont work in this case in pandas
Below is my sample df

enter image description here

Expected O/p:
enter image description here

Any help wil be much appreciated, Thanks

Upvotes: 1

Views: 3091

Answers (3)

aguay091
aguay091

Reputation: 62

nlargest will work:

df[['col1','col2' ]].drop_duplicates(keep='last').nlargest(10, 'col2')

Upvotes: 1

harpan
harpan

Reputation: 8631

You need:

df.sort_values('val', ascending=False).drop_duplicates('col').head(3).sort_values('col')

Output:

   col  val
0   A   50
2   B   32
3   C   41

Upvotes: 0

BENY
BENY

Reputation: 323276

sort_values then drop_duplicates and we get the tail 3 rows.

df.sort_values('col2').drop_duplicates('col1',keep='last').tail(3)

Upvotes: 4

Related Questions