1

I'm trying to groupby a column in pandas and then sum the groupby.

Here is an example df and my expected output:

d = {'a':[1, 1, 1, 1, 2, 2, 2], 'b': [3, 4, 5, 6, 7,8,9] }
data = pd.DataFrame(data = d)


# should return sum of sum of groups
# correct output would be 42

I know that I can return the sum of groups by using:

data.groupby('a')['b'].transform(sum)

# which returns

0    18
1    18
2    18
3    18
4    24
5    24
6    24
Name: b, dtype: int64
 

However, I'm not sure how to get the sum of the sum of groups. i.e.

# sum of groupby
# group 1: 18
# group 2: 24
# sum of sum of groupby
# 18 + 24 = 42
2
  • 6
    why not summing up all the elements from column b by data['b'].sum()?? Commented Jul 29, 2021 at 11:15
  • Yes.. yes it would. Commented Jul 29, 2021 at 11:36

1 Answer 1

2

You need to use agg in place of transform.

res = data.groupby('a')['b'].agg(sum).sum()

res:

42

data.groupby('a')['b'].agg(sum)

will give you

a
1    18
2    24
Name: b, dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.