Home > Blockchain >  Pandas : Top N results with float and string list
Pandas : Top N results with float and string list

Time:10-01

idx float str list
1   -0.2  [A,B]
1   -0.1  [A,D]
1    0.2  [B,C]

To know the best result : df.loc[df['float'].idxmax()]['str list']

How can I have the top 2 idxmax results?

nlargest gives me error

CodePudding user response:

Use DataFrame.nlargest:

s = df.nlargest(2, 'float')['str list']
print (s)
2    [B,C]
1    [A,D]
Name: str list, dtype: object

Or sorting with select top N values:

df.sort_values('float', ascending=False)['str list'].head(2)
  • Related