Change column value based on difference of another column

I have a dataframe like below:

    timestamp        type        value
0   1629453592426    0           10
1   1629453594429    0           120000
2   1629453654582    0           110000
3   1629453658662    1           22
4   1629453661342    1           44
5   1629453686644    0           115000
6   1629453689849    0           50

I want the value values to be 0 when the timestamp difference with the previous row is less than 60000 if the value is greater than 100000 or less than -100000 provided that type didn’t change.

So the expected output is:

    timestamp        type        value
0   1629453592426    0           10
1   1629453594429    0           0
2   1629453654582    0           110000
3   1629453658662    1           22
4   1629453661342    1           44
5   1629453686644    0           115000
6   1629453689849    0           50

I tried to do it by creating a timestamp_jump column with:

df["timestamp_jump"] = df["timestamp"].diff().gt(60000).cumsum()

And then grouping the dataframe by timestamp_jump and type columns:

df.groupby(["timestamp_jump", "type"])[["value"]]

But couldn’t figure out how to do the other steps. How can I achieve my expected dataframe?

Answer

Another way:

df.loc[df['type'].eq(df['type'].shift())
       & df['timestamp'].diff().lt(60000)
       & df['value'].abs().gt(100000), 'value'] = 0

Output:

>>> df
       timestamp  type   value
0  1629453592426     0      10
1  1629453594429     0       0
2  1629453654582     0  110000
3  1629453658662     1      22
4  1629453661342     1      44
5  1629453686644     0  115000
6  1629453689849     0      50