How to find the difference between rows of cumulative counts while retaining columns

Viewed 28

I have the following data:

machine_id  time_to_failure
430494        1000
430494        700
430494        500
430494        100
430495        1000
430495        200

The time to failure data is counted from a reference day 0 and I would like to turn it into the time since the previous failure:

machine_id  time_to_failure
430494        300
430494        200
430494        400
430494        100
430495        800
430495        200

I tried using groupby and pivoting duplicate rows into new columns for the subtraction. However, I would like to do it in place to preserve other columns.

1 Answers

Let's try with groupby().diff():

df['time_to_failure'] = (df.groupby('machine_id')
                            ['time_to_failure'].diff(-1)
                           .fillna(df['time_to_failure'])
                        )

Output:

   machine_id  time_to_failure
0      430494            300.0
1      430494            200.0
2      430494            400.0
3      430494            100.0
4      430495            800.0
5      430495            200.0
Related