Skip to content
Advertisement

Divide ‘count’ and ‘sum’ inside agg function in pandas

Using groupby and agg, is it possible to, in the same expression, get the ‘count’ divided by ‘sum’ returned?

JavaScript

Advertisement

Answer

Assuming this is a dummy example (else just compute the mean), yes it is possible to combine aggregators using apply:

JavaScript

Better alternative in my opinion:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement