Python数据帧相乘两个时间序列数据帧

2024-06-17 08:34:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧。第一个数据帧具有一年的能量,时间间隔为30分钟。第二个数据帧是30分钟的一天价格数据。在

如何将两个数据帧相乘以得到第二个数据帧在第一个数据帧的每天重复相乘的结果?在

非常感谢你!在


Tags: 数据间隔时间价格能量
1条回答
网友
1楼 · 发布于 2024-06-17 08:34:48

您要做一些事情,比如合并数据帧on time和multiple,首先确保您的日期在这两个数据帧的datetimeindex中。在

MVCE公司:

df1 = pd.DataFrame({'kilowatts':np.random.randint(100,1000,(2*24*365))},index=pd.date_range('2016-01-01',periods=(2*24*365),freq='30T'))
df2 = pd.DataFrame({'Dol_per_KW':np.random.rand(48)},index=pd.date_range('2016-01-01',periods = 48,freq='30T'))

在每个数据帧中为合并键创建时间列。在

^{pr2}$

合并和倍增:

df_out = df1.merge(df2, on='Time',right_index=True)\
            .eval('cost = kilowatts * Dol_per_KW', inplace=False)\
            .sort_index()

我们每天打印10:00和10:30进行验证。在

print(df_out.loc[df_out.index.hour == 10].head(10))

输出:

                     kilowatts      Time  Dol_per_KW        cost
2016-01-01 10:00:00        187  10:00:00    0.460365   86.088217
2016-01-01 10:30:00        743  10:30:00    0.572282  425.205644
2016-01-02 10:00:00        364  10:00:00    0.460365  167.572786
2016-01-02 10:30:00        668  10:30:00    0.572282  382.284482
2016-01-03 10:00:00        170  10:00:00    0.460365   78.262016
2016-01-03 10:30:00        682  10:30:00    0.572282  390.296432
2016-01-04 10:00:00        336  10:00:00    0.460365  154.682572
2016-01-04 10:30:00        451  10:30:00    0.572282  258.099254
2016-01-05 10:00:00        215  10:00:00    0.460365   98.978431
2016-01-05 10:30:00        295  10:30:00    0.572282  168.823237

相关问题 更多 >