1 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
想法是將每組的第一個(gè)值除以GroupBy.transform
and first
、減法1
、multiple100
和最后一輪:
cols = ['Quantity','Volume']
df[['PctCH_Qty','PctCH_Vol']] = (df[cols].div(df.groupby('Symbol')[cols]
.transform('first'), axis=0)
.sub(1)
.mul(100)
.round(2))
print (df)
Date Symbol Quantity Volume PctCH_Qty PctCH_Vol
0 07-04-2020 ACC 1579268 554047 0.00 0.00
1 08-04-2020 ACC 2153627 586886 36.37 5.93
2 09-04-2020 ACC 2658885 1128891 68.36 103.75
3 07-04-2020 ADANIENT 2463525 408903 0.00 0.00
4 08-04-2020 ADANIENT 3237290 255480 31.41 -37.52
5 09-04-2020 ADANIENT 2359528 335061 -4.22 -18.06
添加回答
舉報(bào)