I have a data frame that contains 4 columns: id, color, flag_1 and flag_2:
df = pd.DataFrame({'id': range(0,5),
'color': ['red', 'red', 'blue', 'blue', 'blue'],
'flag_1':[1, 0, 0, 0, 0],
'flag_2':[1, 1, 1, 1, 0]})
Different from this question: Pandas percentage of total with groupby, i want to group by the column color and get the percentage of total of both, flag_1 and flag_2.
The result should look like this data frame:
color flag_1 flag_2
red 0.5 1
blue 0 0.67
I can't seem to figure out how to adapt the code from the cited question that aggregates just one column, to my needs.