无法在Pandas数据框中使用适当的值填充缺失的小时数

时间:2018-12-07 03:06:59

标签: python pandas

我有一个基本上是列表列表的数据集

data = [[(datetime.datetime(2018, 12, 6, 10, 0), Decimal('7.0000000000000000')), (datetime.datetime(2018, 12, 6, 11, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 6, 12, 0), Decimal('43.6666666666666667')), (datetime.datetime(2018, 12, 6, 14, 0), Decimal('8.0000000000000000')), (datetime.datetime(2018, 12, 7, 9, 0), Decimal('12.0000000000000000')), (datetime.datetime(2018, 12, 7, 10, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 7, 11, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 7, 17, 0), Decimal('2.0000000000000000'))], [(datetime.datetime(2018, 12, 6, 10, 0), 28.5), (datetime.datetime(2018, 12, 6, 11, 0), 12.75), (datetime.datetime(2018, 12, 6, 12, 0), 12.15), (datetime.datetime(2018, 12, 6, 14, 0), 12.75), (datetime.datetime(2018, 12, 7, 9, 0), 12.75), (datetime.datetime(2018, 12, 7, 10, 0), 12.75), (datetime.datetime(2018, 12, 7, 11, 0), 12.75), (datetime.datetime(2018, 12, 7, 17, 0), 12.75)]]

它基本上包含两个列表,每个列表都有一个datemetric列。我需要提取每个列表的指标列值,并找到它们之间的相关关系。

注意:每个列表中的日期都相似

所以首先我将每个列表加载到熊猫中并设置日期索引。

data1 = data[0]
data2 = data[1]

df1 = pd.DataFrame(data1)
df1[0] = pd.to_datetime(df1[0], errors='coerce')
df1.set_index(0, inplace=True)

df2 = pd.DataFrame(data2)
df2[0] = pd.to_datetime(df2[0], errors='coerce')
df2.set_index(0, inplace=True)

现在,我合并两个数据框(它们都共享相同的日期)。

df = pd.merge(df1,df2, how='inner', left_index=True, right_index=True)

现在我的数据框看起来像这样

                                     1_x    1_y
0                                              
2018-12-06 10:00:00   7.0000000000000000  28.50
2018-12-06 11:00:00   2.0000000000000000  12.75
2018-12-06 12:00:00  43.6666666666666667  12.15
2018-12-06 14:00:00   8.0000000000000000  12.75
2018-12-07 09:00:00  12.0000000000000000  12.75
2018-12-07 10:00:00   2.0000000000000000  12.75
2018-12-07 11:00:00   2.0000000000000000  12.75
2018-12-07 17:00:00   2.0000000000000000  12.75

但是,如果您看到最终的数据框,则表示缺少小时。我需要确保为误工时数引入适当的值。

现在,我看到了这个示例,该示例讨论了重新索引https://www.tutorialspoint.com/python_pandas/python_pandas_reindexing.htm,但是我不确定如何在示例中复制它。必须使用interpolate设置值,但是此方法仅提供ffillbfillnearest

如何添加带有适当值的缺失小时数?

注意:数据集是一个sql查询输出。为了处理输出中的Decimal类型,我使用了from decimal import Decimal

1 个答案:

答案 0 :(得分:1)

尝试:

df.resample('H').interpolate()

输出:

                          1_x    1_y
0                                    
2018-12-06 10:00:00   7.000000  28.50
2018-12-06 11:00:00   2.000000  12.75
2018-12-06 12:00:00  43.666667  12.15
2018-12-06 13:00:00  25.833333  12.45
2018-12-06 14:00:00   8.000000  12.75
2018-12-06 15:00:00   8.210526  12.75
2018-12-06 16:00:00   8.421053  12.75
2018-12-06 17:00:00   8.631579  12.75
2018-12-06 18:00:00   8.842105  12.75
2018-12-06 19:00:00   9.052632  12.75
2018-12-06 20:00:00   9.263158  12.75
2018-12-06 21:00:00   9.473684  12.75
2018-12-06 22:00:00   9.684211  12.75
2018-12-06 23:00:00   9.894737  12.75
2018-12-07 00:00:00  10.105263  12.75
2018-12-07 01:00:00  10.315789  12.75
2018-12-07 02:00:00  10.526316  12.75
2018-12-07 03:00:00  10.736842  12.75
2018-12-07 04:00:00  10.947368  12.75
2018-12-07 05:00:00  11.157895  12.75
2018-12-07 06:00:00  11.368421  12.75
2018-12-07 07:00:00  11.578947  12.75
2018-12-07 08:00:00  11.789474  12.75
2018-12-07 09:00:00  12.000000  12.75
2018-12-07 10:00:00   2.000000  12.75
2018-12-07 11:00:00   2.000000  12.75
2018-12-07 12:00:00   2.000000  12.75
2018-12-07 13:00:00   2.000000  12.75
2018-12-07 14:00:00   2.000000  12.75
2018-12-07 15:00:00   2.000000  12.75
2018-12-07 16:00:00   2.000000  12.75
2018-12-07 17:00:00   2.000000  12.75