I have a dataframe looking like this, df1:
col1 col2
A 2
A 3
A 4
B 4
B 8
Now, I want to calculate the percentage of the value in col2
per unique item in col1
. Hence I want the result to be:
col1 col2
A 0.22
A 0.33
A 0.33
B 0.33
B 0.67
Hence, the sum of col2
has to be 1 for the unique elements in col1
. Does anyone know how to do this without using for loops?