1

Give Data As

import pandas as pd
import numpy as np
df = pd.DataFrame({"Start": [1, 4, 8, 12], "Stop": [2, 6, 9, 13]})
# Calculate df['lag'] = Previous[Stop] - This[Start]

Lag = NA for first Row. Else Lag = Current Row Start - Previous Row Stop

Output:

df = pd.DataFrame({"Start": [1, 4, 8, 12], "Stop": [2, 6, 9, 13],"lag":[np.nan,2,2,3]})

1 Answer 1

3

You are looking for shift

df['lag'] = df['Start'] - df['Stop'].shift()

   Start  Stop  lag
0      1     2  NaN
1      4     6  2.0
2      8     9  2.0
3     12    13  3.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.