Skip to content
Advertisement

Group two(2) or more categorical data by week(7days) in pandas python

This is how my data looks like:

I want to aggregate this by category, Issue and Date(weekly) to get count of record.

Date: Group, the result should be monday to sunday

Count: ADD, if two or more records have same Name and fall in a same week Date time (if falls on same interval 7 days week)

The desired output is given below:

original data

Category Issue Date
Bakes Back Brake failures 11/28/2022
Machines Oiling of the machines 11/29/2022
Cars windscreen broken 11/30/2022
Cars steering wheel is shaking 11/30/2022
Bakes The tyres are flat 12/01/2022
Machines Normal wear and tear 12/02/2022
Machines Normal wear and tear 12/02/2022
Cars warning lights are on 12/03/2022
Bakes Back Brake failures 12/04/2022
Machines Oiling of the machines 12/05/2022
Cars windscreen broken 12/06/2022
Bakes excessive emissions 12/06/2022
Bakes The tyres are flat 12/07/2022
Machines Normal wear and tear 12/08/2022
Cars warning lights are on 12/09/2022
Bakes Brake pads worn 12/10/2022
Machines Machine is consuming too much oil 12/11/2022
Cars the tyres are wearing unevenly 12/11/2022

I want to have a result similar to this:

required solution

if possible the count should be sorted in descending order

Thanks in advance for your help.

Advertisement

Answer

You can use a crosstab with a weekly period:

JavaScript

Output:

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