I have a data frame with a "group" variable, a "count" variable, and a "total" variable. For each group I want to sum the count column and divide that by the sum of the total column. How do I do this, ideally in one line of code?
Here is an example to work with:
test_dc = {1:{'group':'A','cnt':3,'total':5},
2:{'group':'B','cnt':1,'total':8},
3:{'group':'A','cnt':2,'total':4},
4:{'group':'B','cnt':6,'total':13}
}
test_df = pd.DataFrame.from_dict(test_dc, orient='index')
Expected output (roughly):
group | average
A | 0.55555
B | 0.33333
Edit: changed column name from "count" to "cnt" because there seems to be an existing count() method on groupby objects.