简体   繁体   中英

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
........

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 Name s

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM