且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

划分下一行的值并在数据框中创建列

更新时间:2023-08-28 17:37:52

Series划分列. core.groupby.GroupBy.transform.html"rel =" nofollow noreferrer> 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