How do I get the divided values of two columns that are a result from a groupby method

I currently have a dataframe that was made by the following example code

df.groupby(['col1', 'col2', 'Count'])[['Sum']].agg('sum')

which looks like this

col1 col2 Count Sum
DOG HUSKY 600 1500
CAT CALICO 200 3000
BIRD BLUE JAY 1500 4500

I would like to create a new column which outputs the division of df['Sum'] and df['Count'] The expected data frame would look like this

col1 col2 Count Sum Average
DOG HUSKY 600 1500 2.5
CAT CALICO 200 3000 15
BIRD BLUE JAY 1500 4500 3

Any help would be greatly appreciated

Topic data-analysis pandas python

Category Data Science


This is more of a programming question than a data science question and would therefore be better suited for the stackoverflow stackexchange page, but this can be done in the same way in pandas as you would do normal division:

df["Average"] = df["Sum"] / df["Count"]

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.