I have a table looks like:
c_id soap_spend towel_spend year_spend cluster
c1 1 2 3 1
c2 2 4 6 2
c3 1 2 3 2
c4 3 2 5 1
I want to return two tables.
table 1:
cluster_1 cluster_2
% soap_spend a = (1+3)/(3+5) c= (2+1)/(6+3) -- soap_spend.sum/year_spend.sum
% towel_spend b = (2+2)/(3+5) d= (2+4)/(6+3) -- towel_spend.sum/year_spend.sum
table 2:
use results from table 1
cluster_1 cluster_2
% soap_spend a/mean(soap_spend) c/mean(towel_spend)
% towel_spend b/mean(soap_spend) d/mean(towel_spend)
my code:
cols = ['soap_spend', 'towel_spend']
df.groupby('cluster').apply(df[col].sum()/df['year_spend'].sum()
any suggestions on hwo to fix the code?