I'm trying to create a new column that show the weightage of every product that I has.
Let's say I have the following dataframe that I have pivot:
PRODUCT UNIT_TESTED AVG_YIELD
A 401 82.1042
B 1512 96.0687
C 292 22.7806
D 134 37.0088
using
pd.pivot_table(data = df, index = ['PRODUCT'],
values = ("UNIT_TESTED","AVG_YIELD"),
aggfunc = "sum", margins=True)\
.fillna('')
Now, I want to add a new column WEIGHTAGE for each product.
The calculation:
WEIGHTAGE 'A' = (UNIT_TESTED 'A'/Total of UNIT_TESTED)*100
This is where I'm stuck to put into coding to create a new column.
My desired output:
PRODUCT UNIT_TESTED AVG_YIELD WEIGHTAGE
A 401 82.1042 17.1441
B 1512 96.0687 64.6430
C 292 22.7806 12.4840
D 134 37.0088 5.7289