Skip to content Skip to sidebar Skip to footer

Pandas Get Second Minimum Value From Datetime Column

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

Solution 1:

Use nlargest or nsmallest

For second largest,

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

Solution 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]

Post a Comment for "Pandas Get Second Minimum Value From Datetime Column"