Matthias Gallagher
Matthias Gallagher

Reputation: 471

Python pandas column get rows with highest five numbers

I have a dataframe 'df1' with three columns: 'Name', 'Age', and 'Number' and more than hundreds of rows. I would like to create two new dataframes: 'df2' presenting 'Name' rows with the highest five 'Age' values and 'df3' presenting 'Name' rows with the highest five 'Number' values. I'm thinking doing some .sort() or .max() function but I'm not very sure. Can anyone help me with this please? Thanks in advance!

Upvotes: 0

Views: 155

Answers (1)

Jack
Jack

Reputation: 569

You can use the pandas nlargest function like follows:

df2 = df.nlargest(5, 'Age')
df3 = df.nlargest(5, 'Number')

Just make sure the columns are numeric type.

Upvotes: 3

Related Questions