1 回答
TA贡献1818条经验 获得超3个赞
想法是将每组的第一个值除以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
添加回答
举报