Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding top 10 in a dataframe in Pandas

Tags:

python

pandas

I have a dataframe (df) with approx 800 rows with data like this:

Name:Jason
Age: 45 Ticket:1

Name:Kim
Age: 30 Ticket:0

1 = has a ticket 0 = does not have a ticket

(sorry, that didn't format very well. It's basically 3 columns in the dataframe: Name, Age and Ticket)

Using Pandas, I am wondering what the syntax is for find the Top 10 oldest people who HAVE a ticket

So far I have:

df.sort_values('Age',ascending=False,inplace=True)(data.Ticket==1)
(data.head(10))

I know that's not correct but it shows what the parameters are that I am looking for. Any ideas? Thanks

like image 472
JD2775 Avatar asked May 09 '17 00:05

JD2775


2 Answers

If you only want names of the old people,then

df[df['Ticket'] == 1].sort_values('Age')['Names'].head(10)
like image 161
bigbounty Avatar answered Oct 11 '22 23:10

bigbounty


One of the common ways to do this is to use nlargest method:

df[df.Ticket == 1].nlargest(10, 'Age')['Names']

This way you don't need to do sorting explicitly

like image 40
Teoretic Avatar answered Oct 12 '22 00:10

Teoretic