答案 0 :(得分:1)
尝试:
df["diff"]=df.groupby("GL")["value"].diff().fillna(df["value"])
示例数据的输出:
import pandas as pd
df=pd.DataFrame({"a": list("xyzyyzvx"), "b": [4.6, 33.2, -5, 2.34,1,9,-6.89,23]})
df["diff"]=df.groupby("a")["b"].diff().fillna(df["b"])
>>> df
a b diff
0 x 4.60 4.60
1 y 33.20 33.20
2 z -5.00 -5.00
3 y 2.34 -30.86
4 y 1.00 -1.34
5 z 9.00 14.00
6 v -6.89 -6.89
7 x 23.00 18.40