问题
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