2

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

Manish
  • 117
  • 1
  • 2
  • 12

2 Answers2

4

Use nlargest or nsmallest

For second largest,

series.nlargest(2).iloc[-1]
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
0

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]
sudoxx
  • 423
  • 1
  • 4
  • 9
  • Hey, I had a full working answer your Fast.com question, but the question got deleted. If you reopen the question I can post it? Or contact me from the email in my info, whichever you prefer – abdusco Jul 21 '19 at 06:48
  • I've created a question and wrote a detailed answer for you: https://stackoverflow.com/q/57131422/5298150 – abdusco Jul 21 '19 at 08:11