在熊猫数据框中找到倒数第二个日期

里万

我有数据例如:

  1. 采样日期
  2. 2017年8月29日
  3. 2017年8月29日
  4. 2017年8月29日
  5. 2016/2/28
  6. 2016/2/28
  7. 2014年5月15日

等等。现在我可以找到最大和最小日期

df.Sampled_Date.max()
df.Sampled_Date.min()

但是如何找到第二个最近的日期。即2/28/2016在Python的pandas数据框中。

迪娜

您也可以使用 .argsort()

import pandas as pd

# Generate dates
dates = pd.Series(pd.date_range(start='1/1/2017', periods=5, freq=pd.offsets.MonthEnd(3)))

# Random order
dates = dates.sample(frac=1, random_state=0)

# Get the second 'max' date
dates[dates.argsort() == (len(dates)-2)] # 3   2017-10-31

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章