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

Viewed 30

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
1 Answers

This can be done in the same way in pandas as you would do normal division:

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