Krrkrkrkr
Krrkrkrkr

Reputation: 95

How to return index of several maximum values from pandas dataframe?

This is a dataframe of a name occurrences in the text. Names 'Sage', 'Davies' and 'Asplund' occurs equally amount of time. So I need a query to return all three names.

Name,Occurrence 
Sage,6
Davies,6
Asplund,6
Goodwin,5
Panula,5
Arnold,4
........

Upvotes: 2

Views: 5340

Answers (1)

Zero
Zero

Reputation: 77027

Use, to get rows

In [1876]: df[df['Occurrence'] == df['Occurrence'].max()]
Out[1876]:
      Name  Occurrence
0     Sage           6
1   Davies           6
2  Asplund           6

And, to get the Names

In [1878]: df.loc[df['Occurrence'] == df['Occurrence'].max(), 'Name']
Out[1878]:
0       Sage
1     Davies
2    Asplund
Name: Name, dtype: object

In [1879]: df.loc[df['Occurrence'] == df['Occurrence'].max(), 'Name'].values.tolist()
Out[1879]: ['Sage', 'Davies', 'Asplund']

And, to get the index values

In [1880]: df[df['Occurrence'] == df['Occurrence'].max()].index
Out[1880]: Int64Index([0, 1, 2], dtype='int64')

Upvotes: 5

Related Questions