I have a dataframe df I want to calculate the percentage based on the column total.
Suppose I have:
df = pd.DataFrame({
'ID': range(1, 4),
'col1': [10, 5, 10],
'col2': [15, 10, 15],
'col3': [10, 15, 15],
'total': [35, 30, 40]
})
print(df)
ID col1 col2 col3 total
0 1 10 15 10 35
1 2 5 10 15 30
2 3 10 15 15 40
I want to get:
ID col1 col2 col3 total
0 1 28.57 % 42.85 % 28.57 % 100 %
1 2 16.66 % 33.33 % 50 % 100 %
2 3 25 % 37.5% 37.5 % 100 %