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
If you only want names of the old people,then
df[df['Ticket'] == 1].sort_values('Age')['Names'].head(10)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With