Getting the row with max value in Pandas

Question:

Have a df like that:

id  state     date
21  Attivato  05/07/2015
21  Cessato   02/22/2015

I’d like to have a dataframe with only row with max date in it. How can it be performed?

Asked By: Keithx

||

Answers:

Find the most recent date:

recent_date = df['date'].max()

And then get the dataframe with the recent date:

df[df['date'] == recent_date]

To get the row with Top n dates (say top 2 dates),

top_2 = df['date'].nlargest(2)
df[df['date'].isin(top_2)]
Answered By: Vaishali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.