举个例子:
import pandas as pd
import numpy as np
d = {'Group': [1,1,1,2,2,2], 'P': [100,101,96,200,204,208], 'PL': [99,100,94, 199,203,207]}
df = pd.DataFrame(d)
df
Group P PL
0 1 100 99
1 1 101 100
2 1 96 94
3 2 200 199
4 2 204 203
5 2 208 207
对于“组”列中的每个组,我想将“PL”列中的每个元素除以“P”列中的第一个元素。这些值将存储在新列中
所需的输出如下所示:
Group P PL Division
0 1 100 99 0.99
1 1 101 100 1
2 1 96 94 0.94
3 2 200 199 0.995
4 2 204 203 1.015
5 2 208 207 1.035