I have following dataframe in pandas
Date tank hose quantity count set flow
01-01-2018 1 1 20 100 211 12.32
01-01-2018 1 2 20 200 111 22.32
01-01-2018 1 3 20 200 123 42.32
02-01-2018 1 1 10 100 211 12.32
02-01-2018 1 2 10 200 111 22.32
02-01-2018 1 3 10 200 123 42.32
I want to calculate percentage of quantity
and count
grouping by Date
and tank
. My desired dataframe
Date tank hose quantity count set flow perc_quant perc_count
01-01-2018 1 1 20 100 211 12.32 33.33 20
01-01-2018 1 2 20 200 111 22.32 33.33 40
01-01-2018 1 3 20 200 123 42.32 33.33 40
02-01-2018 1 1 10 100 211 12.32 25 20
02-01-2018 1 2 20 200 111 22.32 50 40
02-01-2018 1 3 10 200 123 42.32 25 40
I am doing following to achieve this
test = df.groupby(['Date','tank']).apply(lambda x:
100 * x / float(x.sum()))