【发布时间】:2021-10-25 17:52:41
【问题描述】:
我希望扁平化某些流程。基本上看的是紧随其后的重复。假设我有一个数据框:
d = {'time': [12-08-2020, 13-08-2020, 14-08-2020, 15-08-2020, 16-08-2020], 'state': [off, on, on, on, off]}
df = pd.DataFrame(data=d)
然后我会使用time.shift() 创建“time_end”列。基本上是下一行的时间。结果:
time state time_end
0 12-08-2020 off 13-08-2020
1 13-08-2020 on 14-08-2020
2 14-08-2020 on 15-08-2020
3 15-08-2020 on 16-08-2020
4 16-08-2020 off NaN
我现在的问题是,如何将其展平,使其实际上变成这样的 3 行:
time state time_end
0 12-08-2020 off 13-08-2020
1 13-08-2020 on 16-08-2020
4 16-08-2020 off NaN
对于我的代码,如果它们后面跟着另一个 on,我不需要重复。任何帮助将不胜感激。
【问题讨论】:
标签: python pandas time-series