Skip to content
Advertisement

Python (Pandas) pivot datframe, some sums keeping the order

I’m trying to get from a to b. I got a Pandas data frame similar to the a below.

JavaScript

I realize I’ve backed myself into a corner by computing sums in a flat file. I’m new to Python. I guess I should create the sums when I’m done pivoting?

What I am stuck in is this wrong b struggle,

JavaScript

I would like to get to something like this b,

JavaScript

I’m not sure what to search for (some of the maybe relevant questions has way too much complexity for my to be able to extract what I need, at least at the moment). I’ve looked a lot at this answer, maybe I should find a way using .groupby()

Advertisement

Answer

Maybe you can compute the sum afterwards:

JavaScript

Prints:

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