0

I'm trying to compare average sales of a weekend and weekday in python.

Suppose I have a dataset

 Order Date  Units Sold day_week
2017-07-01  100 Sat
2017-07-02  100 Sun
2017-07-03  90  Mon
2017-07-04  90  Tue
2017-07-05  90  Wed
2017-07-06  90  Thu
2017-07-07  90  Fri
2017-07-08  80  Sat
2017-07-09  80  Sun
2017-07-10  100 Mon
2017-07-11  100 Tue
2017-07-12  100 Wed
2017-07-13  100 Thu
2017-07-14  100 Fri

I want to compare (average sales of weekend that is sat and sun) with (average sales of weekdays), but individually like (1st and 2nd with 3,4,5,6,7 sales) and (8,9 with 10,11,12,13,14)

So in 1st week, weekend average sales (100) will be more than weekday average sales(90) and in 2nd week , weekend average sales (80) will be less than weekday average sales (100)

Kanika Singhal
  • 253
  • 1
  • 2
  • 10

1 Answers1

0

Ok, here's assuming your data is in a DataFrame format, but the date/time is simple str (i.e. not datetime):

import pandas as pd

# setting up part of your dataset

df = pd.DataFrame.from_dict({
    'date':['2017-07-01','2017-07-02','2017-07-03','2017-07-04'],
    'units_sold': [100,100,90,90],
    'day_week': ['Sat','Sun','Mon','Tue']}
)

# defining a new column to help us, grouping by it and then summing:

df['is_weekend']=df['day_week'].apply(lambda x: x in {'Sat','Sun'})
df.groupby('is_weekend').mean()

Also, in the future, it's good conduct to write the code that generates your dataset (or a small part of it), otherwise the reader has to do it itself.

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32