Skip to content
Advertisement

How to get the Toggle flag value and sum of rows between the flag toggle

In continuation to my previous Question I need some more help.

The dataframe is like

JavaScript

I am trying to have the flag state value along with the toggle count, Means flag toggling state. The output should look like this

JavaScript

Can someone help with this?

Advertisement

Answer

Create helper Series with shift and cumsum and aggregate sum, last remove helper first level of MultiIndex by first reset_index:

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