Multiple Aggregate Functions based on Multiple Columns in Pandas

Question:

I am working with a Pandas df in Python. I have the following input df:

Color   Shape   Value
Blue    Square  5
Red     Square  2
Green   Square  7
Blue    Circle  9
Blue    Square  2
Green   Circle  6
Red     Circle  2
Blue    Square  5
Blue    Circle  1

I would like the following output:

Color   Shape   Count   Sum
Blue    Square  3       12
Red     Square  1       2
Green   Square  1       7
Blue    Circle  2       10
Green   Circle  1       6
Red     Circle  1       2

Looking for something like pivot_table() but do not want the hierarchical index.

Asked By: pacificdune

||

Answers:

The problem I am having is associated with indexing more than pivot tables. To remove the multiple index a simple:

df.reset_index()

does the trick just fine.

Answered By: pacificdune
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.