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

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

Add incrementing seconds to DateTime column Pandas

问题

以下是翻译好的内容:

  1. 我有以下类型的`df`
  2. Date_time Col1
  3. 0 2023-03-04 10:30:00 10
  4. 1 2023-03-04 10:30:00 11
  5. 2 2023-03-04 10:30:00 21
  6. 3 2023-03-04 10:30:00 54
  7. 4 2023-03-04 10:30:00 12
  8. 5 2023-03-04 10:30:00 13
  9. 6 2023-03-04 10:30:00 21
  10. ...
  11. 58 2023-03-04 10:30:00 22
  12. 59 2023-03-04 10:30:00 21
  13. 60 2023-03-04 10:31:00 25
  14. 61 2023-03-04 10:31:00 21
  15. ...
  16. 由于某种原因,`df`中的秒数始终为`0`,但实际上它们必须递增`1`秒。
  17. 我想要将`Date_time`列添加`1`秒的递增,并保持递增直到`59`秒,然后在分钟变化时重置它。请参见下面的期望结果:
  18. Date_time Col1
  19. 0 2023-03-04 10:30:00 10
  20. 1 2023-03-04 10:30:01 11
  21. 2 2023-03-04 10:30:02 21
  22. 3 2023-03-04 10:30:03 54
  23. 4 2023-03-04 10:30:04 12
  24. 5 2023-03-04 10:30:05 13
  25. 6 2023-03-04 10:30:06 21
  26. ....
  27. 58 2023-03-04 10:30:58 22
  28. 59 2023-03-04 10:30:59 21
  29. 60 2023-03-04 10:31:00 25
  30. 61 2023-03-04 10:31:01 21
  31. ...

希望这能满足你的需求。

英文:

I have the following type of df:

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

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.

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

答案1

得分: 1

使用 groupby.cumcountTimedeltaIndex

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

输出结果:

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

Use a groupby.cumcount and TimedeltaIndex:

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

Output:

  1. Date_time Col1
  2. 0 2023-03-04 10:30:00 10
  3. 1 2023-03-04 10:30:01 11
  4. 2 2023-03-04 10:30:02 21
  5. 3 2023-03-04 10:30:03 54
  6. 4 2023-03-04 10:30:04 12
  7. 5 2023-03-04 10:30:05 13
  8. 6 2023-03-04 10:30:06 21
  9. ...
  10. 58 2023-03-04 10:30:58 22
  11. 59 2023-03-04 10:30:59 21
  12. 60 2023-03-04 10:31:00 25
  13. 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:

确定