熊猫从datetime列获取第二个最小值

时间:2019-07-21 06:19:30

标签: python-3.x pandas dataframe

我有一个带有DateTime列的数据框,我可以通过使用

获得最小值
df['Date'].min()

如何获得第二,第三...最小值

2 个答案:

答案 0 :(得分:0)

使用nlargestnsmallest

第二大,

series.nlargest(2).iloc[-1]

答案 1 :(得分:0)

确保您的日期最早在日期时间:

df['Sampled_Date'] = pd.to_datetime(df['Sampled_Date'])

然后删除重复项,取nsmallest(2),并取其最后一个值:

df['Sampled_Date'].drop_duplicates().nsmallest(2).iloc[-1]