Finding the top 10 in a dataframe in Pandas

I have a dataframe (df) with approximately 800 lines with data like this:

Name: Jason
Age: 45 Ticket: 1

Name: Kim
Age: 30 Ticket: 0

1 = has a ticket 0 = has no ticket

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

Using Pandas I am wondering what is the syntax for finding the Top 10 Oldest People with a Ticket

So far I have:

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

      

I know what is wrong, but it shows what parameters I am looking for. Any ideas? Thanks to

+3


source to share


2 answers


If you only want the names of old people, then



df[df['Ticket'] == 1].sort_values('Age')['Names'].head(10)

      

+3


source


mask, sorting, head



df[df.Ticket == 1].sort_values('Age').head(10)

      

+3


source







All Articles