更新时间:2023-08-28 17:37:52
由GroupBy.transform
和
Divide column by Series
created by GroupBy.transform
with GroupBy.first
:
df['raise'] = df['value'].div(df.groupby('id')['value'].transform('first'))
print (df)
id value raise
0 1 100 1.000000
1 1 150 1.500000
2 1 200 2.000000
3 1 250 2.500000
4 2 300 1.000000
5 2 350 1.166667
6 2 400 1.333333
7 2 550 1.833333