例如DF,其中包含跨时间戳的执行次数。
DateTime Execution
0 2023-04-03 07:00:00 4
1 2023-04-03 10:00:00 1
2 2023-04-03 12:00:00 1
3 2023-04-03 14:00:00 1
4 2023-04-03 18:00:00 1
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5080 entries, 0 to 5079
下面是我想要实现的输出
DateTime Execution
0 2023-04-03 07:00:00 1
1 2023-04-03 08:00:00 1
2 2023-04-03 09:00:00 1
3 2023-04-03 10:00:00 1
4 2023-04-03 10:00:00 1
5 2023-04-03 12:00:00 1
6 2023-04-03 14:00:00 1
7 2023-04-03 18:00:00 1
因此希望将每个执行次数超过 1 次的时间分布到均匀的每小时时间戳。
我尝试过:如何在Python中按排名在数据帧中的行之间均匀分配“供应”?但这并没有给出所需的输出。
尝试过此操作,但它仅用于排列根据给定列中的值均匀排列数据框行
Index.repeat
和 DataFrame.loc
来重复行,设置 1
并通过 to_timedelta
和 GroupBy.cumcount
添加小时数:
out = (df.loc[df.index.repeat(df['Execution'])]
.assign(Execution=1,
DateTime = lambda x: x['DateTime'] +
pd.to_timedelta(x.groupby(level=0).cumcount(), unit='H'))
.reset_index(drop=True))
print (out)
DateTime Execution
0 2023-04-03 07:00:00 1
1 2023-04-03 08:00:00 1
2 2023-04-03 09:00:00 1
3 2023-04-03 10:00:00 1
4 2023-04-03 10:00:00 1
5 2023-04-03 12:00:00 1
6 2023-04-03 14:00:00 1
7 2023-04-03 18:00:00 1