Warm tip: This article is reproduced from serverfault.com, please click

How do subtraction between timestamp two rows per two with shift- pandas 蟒

(How do subtraction between timestamp two rows per two with shift - Pandas Python)

发布于 2020-11-29 13:21:19

我想在pandas python中用date_time减去,但移动两行,我不知道该函数

Timestamp
2020-11-26 20:00:00
2020-11-26 21:00:00
2020-11-26 22:00:00
2020-11-26 23:30:00

解释:

(2020-11-26 21:00:00) - (2020-11-26 20:00:00)
(2020-11-26 23:30:00) - (2020-11-26 22:00:00)

结果必须是:

01:00:00
01:30:00
Questioner
Thony Nadhir
Viewed
11
Dani Mesejo 2020-11-29 21:27:58

IIUC,你想迭代两个大块并找到不同之处,一种方法是:

res = df.groupby(np.arange(len(df)) // 2).diff().dropna()
print(res)

输出

        Timestamp
1 0 days 01:00:00
3 0 days 01:30:00