Reputation: 149
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
Any help wil be much appreciated, Thanks
Upvotes: 1
Views: 3091
Reputation: 62
nlargest
will work:
df[['col1','col2' ]].drop_duplicates(keep='last').nlargest(10, 'col2')
Upvotes: 1
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
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