import pandas as pd
df = pd.DataFrame({'Beaver Valley': [923.4, 100, 200, 300, 400, 500, 600],
'DAY': pd.date_range(start='2017-05-01', periods=7)})
# By default, df.reset_index() reinserts the index of df as a column into df, which is what we need here.
df2 = df.set_index('DAY')
# To make sure the last day gets resampled into 24 hour-long intervals,
# append a NaN row before resampling (there may be a more readable way of doing this):
df3 = df2.reindex(pd.date_range(start=df2.index[0], periods=df2.shape[0]+1))
df3.index.rename('DAY', inplace=True)
df4 = df3.resample('h').ffill().reset_index()
df4.head()
# Output:
# DAY Beaver Valley
# 0 2017-05-01 00:00:00 923.4
# 1 2017-05-01 01:00:00 923.4
# 2 2017-05-01 02:00:00 923.4
# 3 2017-05-01 03:00:00 923.4
# 4 2017-05-01 04:00:00 923.4
df4.tail()
# Output:
# DAY Beaver Valley
# 164 2017-05-07 20:00:00 600.0
# 165 2017-05-07 21:00:00 600.0
# 166 2017-05-07 22:00:00 600.0
# 167 2017-05-07 23:00:00 600.0
# 168 2017-05-08 00:00:00 NaN
如果我没弄错,您希望将数据帧行重新采样到每小时一次的频率,并向前填充“Beaver Valley”值,以填充重新采样创建的每小时时隙。下面是一个可运行的示例,我认为它可以满足您的需要,使用不同的Beaver Valley值来说明正向填充的结果:
如果需要,现在可以从最终数据框中删除最终占位符行:
^{pr2}$Date
将Date
移到索引asfreq
或{ffill
向前重复现有值reindex_axis
只是为了按相同的顺序恢复列示例
^{pr2}$应用建议的解决方案
相关问题 更多 >
编程相关推荐