简体   繁体   English

在 Pandas 中获取具有最大值的行

[英]Getting the row with max value in Pandas

Have a df like that:有一个这样的 df:

在此处输入图像描述

I'd like to have a dataframe with only row with max date in it.我想要一个数据框,其中只有包含最大日期的行。 How can it be performed?如何执行?

Thanks!谢谢!

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),要获得前 n 个日期的行(比如前 2 个日期),

top_2 = df['date'].nlargest(2)
df[df['date'].isin(top_2)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM