需要编写函数以在 pivot table 中找到总数的百分比
need writing function to find percentage from total in pivot table
例如这个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
我需要写函数来得到这个table(总额率):
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
IIUC
df.update(df.loc[:,df.columns.str.contains('Amount')].div(df.Total,0))
df
Out[41]:
Class Amount_1 Amount_2 Amount_3 Total
0 A 0.156250 0.656250 0.187500 64
1 B 0.252632 0.115789 0.652632 95
2 C 0.125000 0.696429 0.178571 112
尝试:
from sklearn.preprocessing import normalize
df[["Amount_1", "Amount_2", "Amount_3"]]=normalize(df[["Amount_1", "Amount_2", "Amount_3"]], axis=1, norm="l1")
输出:
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
例如这个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
我需要写函数来得到这个table(总额率):
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
IIUC
df.update(df.loc[:,df.columns.str.contains('Amount')].div(df.Total,0))
df
Out[41]:
Class Amount_1 Amount_2 Amount_3 Total
0 A 0.156250 0.656250 0.187500 64
1 B 0.252632 0.115789 0.652632 95
2 C 0.125000 0.696429 0.178571 112
尝试:
from sklearn.preprocessing import normalize
df[["Amount_1", "Amount_2", "Amount_3"]]=normalize(df[["Amount_1", "Amount_2", "Amount_3"]], axis=1, norm="l1")
输出:
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