My data looks like the below
| Position | Enrolled | Retained
| -------- | -------- |
| FT | 1 | 1
| PT | 1 | 0
| S | 1 | 1
| FT | 1 | 0
| S | 1 | 0
| PT | 1 | 1
I want to calculate the percent retained (Retained/Enrolled) by group.
I'd like my output to look like
FT .50
PT .50
S .50
I tried the following:
df2.groupby(by=['Position']).(df2['Enrolled'].sum()/df2['Retained'].sum())
But that's not the correct syntax to accomplish this.
If you use pd.DataFrame.eval
, you can get pretty close (syntactically) to your proposed solution ;)
out = df2.groupby("Position").sum().eval("Retained / Enrolled")
out:
Position
FT 0.5
PT 0.5
S 0.5
dtype: float64