![](/img/trans.png)
[英]Python Pandas: How to subtract values in two non-consecutive rows in a specific column of a dataframe from one another
[英]Subtract consecutive timeframes in a pandas dataframe given the values of another column
我有一個這樣的熊貓數據框:
CustomerId Timestamp
0. a 01-09-2018 00:08:00
1. a 01-09-2018 00:09:00
2. b 01-09-2018 00:11:00
3. b 01-09-2018 00:15:00
我需要計算每個客戶的連續時間戳之間以分鍾為單位的差異,以便最終獲得如下所示的內容:
CustomerId Timestamp Difference
0. a 01-09-2018 00:08:00 -
1. a 01-09-2018 00:09:00 1
2. b 01-09-2018 00:11:00 -
3. b 01-09-2018 00:15:00 4
我一直在嘗試一些循環,但似乎沒有任何效果。 如果有人可以幫助我,我將非常感激:)
與diff
使用groupby
df.groupby('CustomerId').Timestamp.diff().dt.total_seconds()/60
Out[10]:
0.0 NaN
1.0 1.0
2.0 NaN
3.0 4.0
Name: Timestamp, dtype: float64
df['Different']=df.groupby('CustomerId').Timestamp.diff().dt.total_seconds()/60
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.