I think using .cumsum()
would suffice.
import numpy as np
import pandas as pd
data = [["a",12],["a",13],["a",15],["b",32],["b",34],["b",37]]
df = pd.DataFrame(data)
df.columns = ['type', 'time']
df["diff"] = df.groupby("type")["time"].diff().fillna(0)
df["diff"] = df.groupby("type")["diff"].cumsum()
print(df)
>>>
type time diff
0 a 12 0.0
1 a 13 1.0
2 a 15 3.0
3 b 32 0.0
4 b 34 2.0
5 b 37 5.0
BTW, this code also works:
import numpy as np
import pandas as pd
data = [["a",12],["a",13],["a",15],["b",32],["b",34],["b",37]]
df = pd.DataFrame(data)
df.columns = ['type', 'time']
df["diff"] = df['time'] - df.groupby("type")["time"].transform('first')
print(df)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…