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?
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)]
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?
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)]