gpt4 book ai didi

python - 连续数据帧行之间的时间差

转载 作者:行者123 更新时间:2023-12-05 01:22:32 25 4
gpt4 key购买 nike

类似于this问题,我想计算数据帧行之间的时间差。然而,与那个问题不同的是,区别应该是 groupby id。

所以敌人的例子,这个数据框:

df = pd.DataFrame(
{'id': [6,6,6,6,6,10,10,10,10,10],
'timestamp': ['2016-04-01 00:04:00','2016-04-01 00:04:20','2016-04-01 00:04:30',
'2016-04-01 00:04:35','2016-04-01 00:04:54','2016-04-30 13:04:59',
'2016-04-30 13:05:00','2016-04-30 13:05:12','2016-04-30 13:05:20',
'2016-04-30 13:05:51']}
)
df.head()
id timestamp
0 6 2016-04-01 00:04:00
1 6 2016-04-01 00:04:20
2 6 2016-04-01 00:04:30
3 6 2016-04-01 00:04:35
4 6 2016-04-01 00:04:54
5 10 2016-04-30 13:04:59
6 10 2016-04-30 13:05:00
7 10 2016-04-30 13:05:12
8 10 2016-04-30 13:05:20
9 10 2016-04-30 13:05:51

然后我想为差异创建一个列 ΔT,如下所示:

df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S')
df['ΔT'] = df.groupby('id').index.to_series().diff().astype('timedelta64[s]')

AttributeError: 'DataFrameGroupBy' object has no attribute 'index'

预期输出:

    id        timestamp        ΔT
0 6 2016-04-01 00:04:00 0
1 6 2016-04-01 00:04:20 20
2 6 2016-04-01 00:04:30 10
3 6 2016-04-01 00:04:35 5
4 6 2016-04-01 00:04:54 19
5 10 2016-04-30 13:04:59 0
6 10 2016-04-30 13:05:00 1
7 10 2016-04-30 13:05:12 12
8 10 2016-04-30 13:05:20 8
9 10 2016-04-30 13:05:51 31

最佳答案

df.groupby('id')['timestamp'].diff().dt.total_seconds().fillna(0)

关于python - 连续数据帧行之间的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73848347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com