【问题标题】:How to find the range of dates from a datetime column in a dataframe?如何从数据框中的日期时间列中查找日期范围?
【发布时间】:2021-03-28 16:18:42
【问题描述】:

想知道如何在数据框中打印日期范围。似乎这很简单,但我无法在任何地方找到答案。有没有一种简单的方法可以使用 pandas 日期时间模块来做到这一点?

如果这是数据框的一个小版本,例如:

Date Id Value
2020-09-23 14:00:00 4752764 12212
2020-10-25 08:00:00 4752764 12298
2020-10-28 12:00:00 4752764 12291
2020-10-29 18:00:00 4752764 12295

我怎样才能得到这样的输出:

date_range = 2020-09-23 to 2020-10-29

date_range = 23rd of September, 2020 to 29th of October, 2020

感谢任何答案:)

【问题讨论】:

  • .max().min()?
  • 一个不错且简单的解决方案。谢谢@roganjosh! :)

标签: python pandas dataframe datetime


【解决方案1】:

试试这个

df['Date'] = pd.to_datetime(df['Date']) # If your Date column is of the type object otherwise skip this
date_range = str(df['Date'].dt.date.min()) + ' to ' +str(df['Date'].dt.date.max())

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2013-03-04
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多