在 Pandas 中将时间增量添加到 运行 总数

Adding time deltas to a running total in Pandas

所以我的数据框中有两列数据。 TimeDeltasDiffs 和 ActualTime。数据框的第一行在 ActualTime 列中有一个开始时间。我想根据当前行中的时间增量值增量填充 ActualTime 列。

我在此数据帧上使用 apply 和 lambdas 进行其他操作。例如

df_mrx['Log Timestamp'] = df_mrx['Log Timestamp'].apply(lambda x: '00:' + x)

但不确定如何在我迭代时根据以前的值使用它...

如有任何帮助,我们将不胜感激。

您不一定需要使用 .apply()。考虑以下方法。 鉴于下面是 df

       ActualTime           TimeDeltasDiffs
0   2018-04-16 17:06:01      00:00:00
1        0                   00:00:01
2        0                   00:00:00
3        0                   00:00:02

您需要在 TimeDeltasDiffs

上使用 .cumsum()
df['ActualTime'] = df.iloc[0]['ActualTime']
df['ActualTime'] = pd.to_datetime(df['ActualTime']) + pd.to_timedelta(df['TimeDeltasDiffs']).cumsum()

输出:

         ActualTime            TimeDeltasDiffs
0   2018-04-16 17:06:01           00:00:00
1   2018-04-16 17:06:02           00:00:01
2   2018-04-16 17:06:02           00:00:00
3   2018-04-16 17:06:04           00:00:02