Skip to content
Advertisement

Group By – Total Hours and Hours by Category in Python / Pandas

I need to calculate Total Hours and Hours by Status per Week using Python / Pandas GROUP BY.

JavaScript
JavaScript

I can get Total Hours by each Week:

JavaScript

But I don’t know how to also group by Status, so it will be 2 additional columns (On Status Hours and Off Status Hours)

If I add Status column just to the groupby part, it creates extra rows (I understand why)

JavaScript

Output I want:

Week Total Hours On Status Hours Off Status Hours
01/03/2022 – 01/09/2022 13 13 0
01/10/2022 – 01/16/2022 16 5 11
01/17/2022 – 01/23/2022 12 6 6
01/24/2022 – 01/30/2022 11 7 4

Advertisement

Answer

You can use:

JavaScript

Output:

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