For example this table:
list_1=[['A',10,42,12,64],
['B',24,11,62,95],
['C',14,78,20,112]]
labels=['Class','Amount_1','Amount_2','Amount_3','Total']
df=pd.DataFrame(list_1,columns=labels)
df
Class Amount_1 Amount_2 Amount_3 Total
0 A 10 42 12 64
1 B 24 11 62 95
2 C 14 78 20 112
I need write function to get this table (amount rate from total):
Class Amount_1 Amount_2 Amount_3
A 0.156250 0.656250 0.187500
B 0.252632 0.115789 0.652632
C 0.125000 0.696429 0.178571
Try:
from sklearn.preprocessing import normalize
df[["Amount_1", "Amount_2", "Amount_3"]]=normalize(df[["Amount_1", "Amount_2", "Amount_3"]], axis=1, norm="l1")
Outputs:
Class Amount_1 Amount_2 Amount_3 Total
0 A 0.156250 0.656250 0.187500 64
1 B 0.247423 0.113402 0.639175 95
2 C 0.125000 0.696429 0.178571 112