Reputation: 3801
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
Upvotes: 21
Views: 133809
Reputation: 2533
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
Upvotes: 26
Reputation: 294218
mask, sort, head
df[df.Ticket == 1].sort_values('Age').head(10)
Upvotes: 5
Reputation: 17368
If you only want names of the old people,then
df[df['Ticket'] == 1].sort_values('Age')['Names'].head(10)
Upvotes: 30