gpt4 book ai didi

python - 需要使用其列将新的 DateTime 列添加到已有的数据框

转载 作者:行者123 更新时间:2023-12-01 07:45:29 25 4
gpt4 key购买 nike

我有一个数据框,其中包含我计算时间的距离和速度。现在我想添加一列,我可以在其中为第一行指定一个固定的日期时间值,它将自动添加时间并将其设置为日期时间列的下一个值

当前数据框如下所示:

x_coordinate    y_coordinate    z_coordinate    speed   Distance    Time1
-22 -2.28 -0.1 300 1 0.2
-21 -2.28 -0.1 300 1 0.2
-20 -2.28 -0.1 300 1 0.2
-19 -2.28 -0.1 300 1 0.2
-18 -2.28 -0.1 300 1 0.2
-17 -2.28 -0.1 300 1 0.2
-16 -2.28 -0.1 300 1 0.2
-15 -2.28 -0.1 300 1 0.2
-14 -2.28 -0.1 300 1 0.2
-13.2674 -2.601 -0.1 300 0.7998398339667759 0.15996796679335518
-13.039 -3.5743 -0.1 300 0.9997396911196436 0.1999479382239287
-12.7392 -4.5281 -0.1 300 0.9998072214182092 0.19996144428364185
-12.3697 -5.4571 -0.1 300 0.9997856020167519 0.1999571204033504

时间以秒为单位,例如 0.2 秒我以速度 300 毫米/秒、距离 = 1 毫米计算。现在我想添加一个名为 DateTime 的列,该列具有硬编码的第一个 Dattime 值和由时间列计算的连续值,例如

    Datetime
2019-02-21 03:50:39.000 --> this is hardcoded
2019-02-21 03:50:39.200 --> this to be calculated by adding 0.2 seconds from row 1.
and so on

最佳答案

尝试:

df['new_time'] = (pd.to_datetime('2019-02-21 03:50:39.000') + 
pd.to_timedelta(df.Time1.shift().cumsum(), unit='s')
)

输出(df['new_time']):

0    2019-02-21 03:50:39.000000000
1 2019-02-21 03:50:39.200000000
2 2019-02-21 03:50:39.400000000
3 2019-02-21 03:50:39.600000000
4 2019-02-21 03:50:39.800000000
5 2019-02-21 03:50:40.000000000
6 2019-02-21 03:50:40.200000000
7 2019-02-21 03:50:40.400000000
8 2019-02-21 03:50:40.600000000
9 2019-02-21 03:50:40.800000000
10 2019-02-21 03:50:40.959967967
11 2019-02-21 03:50:41.159915905
12 2019-02-21 03:50:41.359877349
Name: new_time, dtype: datetime64[ns]

关于python - 需要使用其列将新的 DateTime 列添加到已有的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com