python - Panda 通过分组连接多个时间序列并扩展缺失数据

标签 python pandas dataframe time-series multi-index

我有从 CSV 文件导入的多个时间序列数据。 这些数据都有时间戳,但时间戳并不总是匹配:

时间序列1:

                      UUT  Data
DateTime                                           
2017-11-21 18:54:31  uut1  1
2017-11-22 02:26:48  uut1  2
2017-11-22 10:19:44  uut1  3
2017-11-22 15:11:28  uut1  6
2017-11-22 23:21:58  uut1  7

时间序列2:

                      UUT  Data
DateTime
2017-11-21 18:47:29  uut2  1
2017-11-22 02:26:49  uut2  2
2017-11-22 10:19:44  uut2  3
2017-11-22 15:17:47  uut2  4
2017-11-22 23:21:58  uut2  5
2017-11-23 07:10:56  uut2  6
2017-11-23 15:15:48  uut2  7
2017-11-24 12:16:58  uut2  9

我可以使用 concat 函数将它们连接在一起并按“UUT”分组,但是,如何用以前的值填充空时隙以使最终表看起来类似于:

DateTime          UUT   Data
11/21/17 18:47:29 uut1  1
11/21/17 18:54:31       1
11/22/17 2:26:48        2
11/22/17 2:26:49        2
11/22/17 10:19:44       3
11/22/17 15:11:28       6
11/22/17 15:17:47       6
11/22/17 23:21:58       7
11/23/17 7:10:56        8
11/23/17 15:15:48       8
11/23/17 15:22:16       9
11/24/17 12:16:58       11 
11/21/17 18:47:29 uut2  1
11/21/17 18:54:31       1
11/22/17 2:26:48        1
11/22/17 2:26:49        2
11/22/17 10:19:44       3
11/22/17 15:11:28       3
11/22/17 15:17:47       4
11/22/17 23:21:58       5
11/23/17 7:10:56        6
11/23/17 15:15:48       7
11/23/17 15:22:16       7
11/24/17 12:16:58       9

或者这个:

DateTime            uut1 uut2
11/21/17 18:47:29   1   1
11/21/17 18:54:31   1   1
11/22/17 2:26:48    2   1
11/22/17 2:26:49    2   2
11/22/17 10:19:44   3   3
11/22/17 15:11:28   6   3
11/22/17 15:17:47   6   4
11/22/17 23:21:58   7   5
11/23/17 7:10:56    8   6
11/23/17 15:15:48   8   7
11/23/17 15:22:16   9   7
11/24/17 12:16:58   11  9

我的最终目标是能够在单个时间序列图上绘制 uut1 和 uut2 数据。

最佳答案

使用index.union查找索引的并集,重新索引数据帧,concat,然后pivot以获得您想要的输出。

i = df1.index.union(df2.index)
df1 = df1.reindex(i).reset_index().bfill().ffill()
df2 = df2.reindex(i).reset_index().bfill().ffill()

df = pd.concat([df1, df2]).pivot('DateTime', 'UUT', 'Data')
df

UUT                  uut1  uut2
DateTime                       
2017-11-21 18:47:29   1.0   1.0
2017-11-21 18:54:31   1.0   2.0
2017-11-22 02:26:48   2.0   2.0
2017-11-22 02:26:49   2.0   2.0
2017-11-22 10:19:44   3.0   3.0
2017-11-22 15:11:28   6.0   4.0
2017-11-22 15:17:47   6.0   4.0
2017-11-22 23:21:58   7.0   5.0
2017-11-23 07:10:56   7.0   6.0
2017-11-23 15:15:48   7.0   7.0
2017-11-24 12:16:58   7.0   9.0

最后,要绘图,请使用 df.plot -

df.plot(subplots=True, drawstyle='steps-post')
plt.show()

enter image description here

关于python - Panda 通过分组连接多个时间序列并扩展缺失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862651/

相关文章:

python - 使用 sklearn 的 PCA

python - 将曲线拟合到 Python 中的直方图

python - 展平 pandas 中的嵌套 json

python - 没有日期的时间增量

python - 在 DataFrame 中插入 Pandas 绘图图像

python - Python 中的性能问题 : os. walk() + filecmp.dircmp().report_full_closure()

python - 如何让像场变大?

python - 将 Python 列表解析为 Pandas DataFrame

python-3.x - 将数据帧中的值与另一个数据帧的多列进行比较,以获取条目以有效方式匹配的列表列表

python-3.x - 如果值超出上下限(异常值处理),则将值的数据框替换为 np.nan