Skip to content Skip to sidebar Skip to footer

Divide Value Of Next Row And Create Column In Dataframe

I have one csv like id,value 1,100 1,150 1,200 1,250 2,300 2,350 2,400 2,450 I want to generate one column based on value of each of the unique id. For Example: first 2 rows for

Solution 1:

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

Post a Comment for "Divide Value Of Next Row And Create Column In Dataframe"