英文:
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.cumcount
和 TimedeltaIndex
:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论