Pandas get second minimum value from datetime column [duplicate]

a 夏天 提交于 2021-01-28 08:08:00

问题


I have a data frame with a DateTime column, I can get minimum value by using

df['Date'].min()

How can I get the second, third... smallest values


回答1:


Use nlargest or nsmallest

For second largest,

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



回答2:


Make sure your dates are in datetime first:

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

Then drop the duplicates, take the nsmallest(2), and take the last value of that:

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


来源:https://stackoverflow.com/questions/57130799/pandas-get-second-minimum-value-from-datetime-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!