简体   繁体   中英

Pandas: calculate the time since last label measurement

My dataset is of the form -

|   Time   | Category|
=====================
|   12:37  |  'one'  | 
|   12:39  |  'two'  | 
|   12:41  |  'two'  | 
|   12:45  |  'one'  |
|   12:46  |  'one'  | 

I want to create a new column that measures the time difference between the current row and the previous time that particular label was recorded, such that the table becomes

|   Time   | Category |  Since_last |
=====================================
|   12:37  |  'one'   |     0 min   |    (0 as it is the first measurement)
|   12:39  |  'two'   |     0 min   | 
|   12:41  |  'two'   |     2 min   | 
|   12:45  |  'one'   |     8 min   |
|   12:46  |  'one'   |     1 min   | 

How would I do this?

Convert your time series to timedelta , then use groupby + diff :

df['Time'] = pd.to_timedelta(df['Time']+':00')
df['Diff'] = df.groupby('Category')['Time'].diff().fillna(0)

print(df)

      Time Category     Diff
0 12:37:00    'one' 00:00:00
1 12:39:00    'two' 00:00:00
2 12:41:00    'two' 00:02:00
3 12:45:00    'one' 00:08:00
4 12:46:00    'one' 00:01:00

If string formatting is important to you:

df['Diff'] = df['Diff'].apply(lambda x: f'{int(x.seconds/60)} min')

print(df)

      Time Category   Diff
0 12:37:00    'one'  0 min
1 12:39:00    'two'  0 min
2 12:41:00    'two'  2 min
3 12:45:00    'one'  8 min
4 12:46:00    'one'  1 min

Convert time

df['Time'] = pd.to_datetime(df['Time'],format= '%H:%M' ).dt.time

Use Groupby and Diff

df=pd.concat([df.Time, df.groupby('Category').Time.diff()],
          axis=1, keys=['Time','Diff']).fillna(0)

Convert to mins

df['Diff']=df['Diff'].apply(lambda x: f'{int(x.seconds/60)} min')

Output

    Time    Category
0   12:37:00    one
1   12:39:00    two
2   12:41:00    two
3   12:45:00    one
4   12:46:00    one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM