Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the row with max value in Pandas

Tags:

python

pandas

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?

like image 830
Keithx Avatar asked Oct 27 '25 10:10

Keithx


1 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)]
like image 97
Vaishali Avatar answered Oct 28 '25 22:10

Vaishali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!