如何解决熊猫:即使缺少值也要绘制时间序列
我有一系列带有时间戳的事件的数据集。我想绘制在每个时间间隔发生的事件数(几个图,例如“每月”或“每天”或“每小时”)。这些图是使用pandas
尤其是groupby()
我已经知道如何执行此操作,但是这些图忽略了没有事件的日期范围。例如,在下面的示例中,2020-08-16没有事件,因此不会绘制日期。 相反,我希望以0计数。
我知道该如何使用旧的方法:我可以使用Python循环等自己对数据进行后处理。但这听起来像pandas
应该可以更高效地完成工作,但是我无法找出方法。
我创建了一个最小的可复制代码段: https://gist.github.com/jlumbroso/50afaa12d8af8dac615331d515f0f0ff
并在此处提供了一个说明性示例:
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
我找到了这些相关问题,但是我无法从中得出答案:
- Pandas Time Series DataFrame Missing Values
- pandas plot time-series with minimized gaps
- Pandas Resampling error: Only valid with DatetimeIndex or PeriodIndex
- Groupby and resample timeseries so date ranges are consistent
感谢您的帮助!
解决方法
好的,您需要使用Resample。 让我们使用您的数据
content = """0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
"""
from io import StringIO
df = pd.read_csv(StringIO(content),sep=" ",header=None,index_col=0)
print(df)
1 2
0
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
使用第一列(如index),然后将其删除:
df = df.set_index(pd.DatetimeIndex(df.iloc[:,0]))
df.drop(df.columns[0],1,inplace=True)
df
2
1
2020-08-15 16:34:15.838169 False
2020-08-17 14:25:08.778913 True
2020-08-19 07:44:07.514456 False
2020-08-19 14:48:29.160890 True
2020-08-20 03:26:00.479444 False
2020-08-20 10:57:52.904366 False
2020-08-20 19:17:45.079390 True
2020-08-20 23:38:41.369156 False
2020-08-21 12:21:54.340702 True
2020-08-24 19:42:13.458472 False
2020-08-24 23:09:39.369394 True
2020-08-25 16:35:05.059722 False
2020-08-26 01:31:29.243435 True
2020-08-26 03:28:25.418322 True
2020-08-27 12:42:43.905486 True
2020-08-31 10:35:57.143843 False
2020-09-02 11:32:54.219081 True
2020-09-02 14:07:05.544261 False
2020-09-03 08:05:32.133082 False
2020-09-10 15:28:46.725916 True
2020-09-12 00:57:58.558055 True
2020-09-13 21:28:02.450837 True
例如按天,总和和绘图
重采样df.resample('D').sum().plot()
请注意,如果您具有列名,则很有用:
content = """Date Condition
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
"""
from io import StringIO
df = pd.read_csv(StringIO(content),index_col=0)
print(df)
Date Condition
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
和
df = df.set_index(pd.DatetimeIndex(df['Date']))
df.drop(["Date"],inplace=True)
df
Condition
Date
2020-08-15 16:34:15.838169 False
2020-08-17 14:25:08.778913 True
2020-08-19 07:44:07.514456 False
2020-08-19 14:48:29.160890 True
2020-08-20 03:26:00.479444 False
2020-08-20 10:57:52.904366 False
2020-08-20 19:17:45.079390 True
2020-08-20 23:38:41.369156 False
2020-08-21 12:21:54.340702 True
2020-08-24 19:42:13.458472 False
2020-08-24 23:09:39.369394 True
2020-08-25 16:35:05.059722 False
2020-08-26 01:31:29.243435 True
2020-08-26 03:28:25.418322 True
2020-08-27 12:42:43.905486 True
2020-08-31 10:35:57.143843 False
2020-09-02 11:32:54.219081 True
2020-09-02 14:07:05.544261 False
2020-09-03 08:05:32.133082 False
2020-09-10 15:28:46.725916 True
2020-09-12 00:57:58.558055 True
2020-09-13 21:28:02.450837 True
df.resample('D').sum().plot()
,
问:为什么将列设置为索引后删除该列?
A :因为在此之前,您需要两次访问该列,例如索引和维度/属性/数据:
Date Condition
Date
2020-08-15 16:34:15.838169 2020-08-15 16:34:15.838169 False
2020-08-17 14:25:08.778913 2020-08-17 14:25:08.778913 True
2020-08-19 07:44:07.514456 2020-08-19 07:44:07.514456 False
2020-08-19 14:48:29.160890 2020-08-19 14:48:29.160890 True
2020-08-20 03:26:00.479444 2020-08-20 03:26:00.479444 False
2020-08-20 10:57:52.904366 2020-08-20 10:57:52.904366 False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。