Calculating the percentage of an outcome per group

105 views Asked by At

Have a data frame of a predictive model output that is seperated into tertiles (low, medium, and high risk). I want to calculate the percentage of people in each risk zone that have the outcome of interest.

import pandas as pd

data = {'risk_group':  ["medium", "low", "high", "low", "high", "high", ....],
        'outcome': [1, 0, 1, 0, 1, 1, ....}

df = pd.DataFrame (data, columns = ['risk_group','outcome'])

theoretical desired output is a dataframe that has

low : 12% w/ outcome
medium : 34% w/ outcome
high: 78% w/ outcome
1

There are 1 answers

0
CainĂ£ Max Couto da Silva On BEST ANSWER

Use:

df.groupby('risk_group').outcome.apply(lambda x: x.sum()/x.size * 100)