python-3.x - 如何使用python按日期拆分列
问题描述
df.head(7)
df
Month,ward1,ward2,...ward30
Apr-19, 20, 30, 45
May-19, 18, 25, 42
Jun-19, 25, 19, 35
Jul-19, 28, 22, 38
Aug-19, 24, 15, 40
Sep-19, 21, 14, 39
Oct-19, 15, 18, 41
至:
Month, ward1
Apr-19, 20
May-19, 18
Jun-19, 25
Jul-19, 28
Aug-19, 24
Sep-19, 21
Oct-19, 15
Month,ward2
Apr-19, 30
May-19, 25
Jun-19, 19
Jul-19, 22
Aug-19, 15
Sep-19, 14
Oct-19, 18
Month, ward30
Apr-19, 45
May-19, 42
Jun-19, 35
Jul-19, 38
Aug-19, 40
Sep-19, 39
Oct-19, 41
python - 如何使用pandas在python中按日期分组?
我有数据框 df ,其中包含一个日期时间和 30 个其他列,我想按日期拆分这些列,这些列附在 pandas 中的每一列中,但我面临一些困难。
解决方案
尝试使用字典理解来保存单独的数据框。
dfs = {col : df.set_index('Month')[[col]] for col in (df.set_index('Month').columns)}
print(dfs['ward1'])
ward1
Month
Apr-19 20
May-19 18
Jun-19 25
Jul-19 28
Aug-19 24
Sep-19 21
Oct-19 15
print(dfs['ward30'])
ward30
Month
Apr-19 45
May-19 42
Jun-19 35
Jul-19 38
Aug-19 40
Sep-19 39
Oct-19 41
推荐阅读
- sql - 如何使用标准 SQL 将日期时间转换为 Google Bigquery 中的日期,或者如何从日期时间的 where 语句中提取年份?
- excel - 从共享驱动器运行宏时出现运行时错误 5
- google-drive-api - 有没有办法列出谁查看了文件(以及何时)?
- jquery - Ajax 请求响应未显示在 div 部分
- ios - iOS Cordova 应用程序在启动画面后挂起
- c - 为什么 inet_ntoa 中的缓冲区大小为 18?
- javascript - 全屏后textarea文本意外向下移动
- xslt - XSLT 1.0 - 比较日期值并偏移 -15 天
- amazon-web-services - 在 AWS ECS 中运行 docker 并传递 env 文件
- flutter - 将来自 API 的 JSON 响应反序列化为 Dart 中的类型