在Pandas中将DateTime列添加递增的秒数

huangapple go评论74阅读模式
英文:

Add incrementing seconds to DateTime column Pandas

问题

以下是翻译好的内容:

我有以下类型的`df`:

        Date_time         Col1
    0 2023-03-04 10:30:00  10
    1 2023-03-04 10:30:00  11
    2 2023-03-04 10:30:00  21
    3 2023-03-04 10:30:00  54
    4 2023-03-04 10:30:00  12 
    5 2023-03-04 10:30:00  13
    6 2023-03-04 10:30:00  21
    ...
    58 2023-03-04 10:30:00  22
    59 2023-03-04 10:30:00  21
    60 2023-03-04 10:31:00  25
    61 2023-03-04 10:31:00  21
    ...

由于某种原因,`df`中的秒数始终为`0`,但实际上它们必须递增`1`秒。

我想要将`Date_time`列添加`1`秒的递增,并保持递增直到`59`秒,然后在分钟变化时重置它。请参见下面的期望结果:

        Date_time         Col1
    0 2023-03-04 10:30:00  10
    1 2023-03-04 10:30:01  11
    2 2023-03-04 10:30:02  21
    3 2023-03-04 10:30:03  54
    4 2023-03-04 10:30:04  12 
    5 2023-03-04 10:30:05  13
    6 2023-03-04 10:30:06  21
    ....
    58 2023-03-04 10:30:58  22
    59 2023-03-04 10:30:59  21
    60 2023-03-04 10:31:00  25
    61 2023-03-04 10:31:01  21
    ...

希望这能满足你的需求。

英文:

I have the following type of df:

    Date_time         Col1
0 2023-03-04 10:30:00  10
1 2023-03-04 10:30:00  11
2 2023-03-04 10:30:00  21
3 2023-03-04 10:30:00  54
4 2023-03-04 10:30:00  12 
5 2023-03-04 10:30:00  13
6 2023-03-04 10:30:00  21
...
58 2023-03-04 10:30:00  22
59 2023-03-04 10:30:00  21
60 2023-03-04 10:31:00  25
61 2023-03-04 10:31:00  21
...

For some reason, the seconds show 0 throughout the df, however in fact they must be incremented by 1 second.

I would like to add 1 second increment to Date_time column and keep incrementing up to 59 seconds, then reset it as the minute changes. Please see below the desired outcome.

    Date_time         Col1
0 2023-03-04 10:30:00  10
1 2023-03-04 10:30:01  11
2 2023-03-04 10:30:02  21
3 2023-03-04 10:30:03  54
4 2023-03-04 10:30:04  12 
5 2023-03-04 10:30:05  13
6 2023-03-04 10:30:06  21
....
58 2023-03-04 10:30:58  22
59 2023-03-04 10:30:59  21
60 2023-03-04 10:31:00  25
61 2023-03-04 10:31:01  21
...

答案1

得分: 1

使用 groupby.cumcountTimedeltaIndex

df['Date_time'] = pd.to_datetime(df['Date_time'])

df['Date_time'] += pd.TimedeltaIndex(df.groupby('Date_time').cumcount(), unit='s')

输出结果:

             Date_time  Col1
0  2023-03-04 10:30:00    10
1  2023-03-04 10:30:01    11
2  2023-03-04 10:30:02    21
3  2023-03-04 10:30:03    54
4  2023-03-04 10:30:04    12
5  2023-03-04 10:30:05    13
6  2023-03-04 10:30:06    21
...
58 2023-03-04 10:30:58    22
59 2023-03-04 10:30:59    21
60 2023-03-04 10:31:00    25
61 2023-03-04 10:31:01    21
英文:

Use a groupby.cumcount and TimedeltaIndex:

df['Date_time'] = pd.to_datetime(df['Date_time'])

df['Date_time'] += pd.TimedeltaIndex(df.groupby('Date_time').cumcount(), unit='s')

Output:

             Date_time  Col1
0  2023-03-04 10:30:00    10
1  2023-03-04 10:30:01    11
2  2023-03-04 10:30:02    21
3  2023-03-04 10:30:03    54
4  2023-03-04 10:30:04    12
5  2023-03-04 10:30:05    13
6  2023-03-04 10:30:06    21
...
58 2023-03-04 10:30:58    22
59 2023-03-04 10:30:59    21
60 2023-03-04 10:31:00    25
61 2023-03-04 10:31:01    21

huangapple
  • 本文由 发表于 2023年4月4日 17:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定