Skip to content
Advertisement

Python: Calculate week start and week end from daily data in pandas dataframe?

I have a daily dataset for different months. I want to calculate the week start(sunday) and week end(saturday) based on each product type & country and values should be the average for that particular week.

JavaScript

SAMPLE result format:

JavaScript

I tried with groupby but I’m not able to get week start and end for each product and country.

Also Values should be the average(addition) for that particular week. Any help on how to achieve this?

Advertisement

Answer

The first step would be to create a new column for the start date and end date for the date of each row. This can be done by using offsets.Week:

JavaScript

From there you can use groupby to group by the start, end, product and country columns and use the mean aggregation method for the value column:

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