python - 如何从数据框中的日期时间列中查找日期范围?
问题描述
想知道如何在数据框中打印日期范围。似乎这很简单,但我无法在任何地方找到答案。有没有一种简单的方法可以使用 pandas 日期时间模块来做到这一点?
因此,如果这是数据框的一个小版本,例如:
日期 | ID | 价值 |
---|---|---|
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
我很感激任何答案:)
解决方案
尝试这个
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())
推荐阅读
- java - 在 IDE 中删除 Kafka Stream 应用程序的状态目录失败
- apache-spark - 使用 Hive 表迭代 Spark 数据帧
- flutter - 如果不为空或为空,如何检查 SharedPreferences 键?
- c# - 如何在 C# 中将选定的行从 sql 数据库加载到 DataGridView?
- makefile - 未创建 Makefile 对象文件
- shell - 如何使用鱼在终端上记录工作历史记录或日志,包括命令和输出?
- matlab - MATLAB Symprod?需要返回一个产品系列应该返回的函数
- python - 如何获得多列时间戳的最小值
- ionic-framework - ionic 3中每个元素的类似反应
- java - JFrame 的 Oval 被放置在 Rectangles JAVA 后面