Python 使用最新日期从dataframe提取最新数据
我想从数据框中使用最新日期提取数据。 我使用了下面的公式,但是输出是不同的Python 使用最新日期从dataframe提取最新数据,python,pandas,dataframe,Python,Pandas,Dataframe,我想从数据框中使用最新日期提取数据。 我使用了下面的公式,但是输出是不同的 Date Sub Value 10/24/2020 A 1 9/18/2020 A 2 9/21/2020 A 3 9/13/2020 A 4 9/20/2020 A 5 输出:2020-10-24 00:00:00 我正在寻找的输出是 df = df.Date.max() 与和一起使用,用于输出一行数据帧-通过第一个最大日期时间只获取一行: Date
Date Sub Value
10/24/2020 A 1
9/18/2020 A 2
9/21/2020 A 3
9/13/2020 A 4
9/20/2020 A 5
输出:2020-10-24 00:00:00
我正在寻找的输出是
df = df.Date.max()
与和一起使用,用于输出一行数据帧-通过第一个最大日期时间只获取一行:
Date Sub Value
10/24/2020 A 1
或者使用比较max
-如果更像1
max值,则获取多行:
df1 = df.loc[[df.Date.idxmax()]]
要获得与相同
max
值匹配的多行,可以执行以下操作:
df1 = df[df.Date.eq(df.Date.max())]
In [2679]: df[df.Date == df.Date.max()]
Out[2679]:
Date Sub Value
0 2020-10-24 A 1