Skip to content

Walmart is the biggest retail store in the United States. Just like others, they have been expanding their e-commerce part of the business. By the end of 2022, e-commerce represented a roaring $80 billion in sales, which is 13% of total sales of Walmart. One of the main factors that affects their sales is public holidays, like the Super Bowl, Labour Day, Thanksgiving, and Christmas.

In this project, you have been tasked with creating a data pipeline for the analysis of supply and demand around the holidays, along with conducting a preliminary analysis of the data. You will be working with two data sources: grocery sales and complementary data. You have been provided with the grocery_sales table in PostgreSQL database with the following features:

grocery_sales

  • "index" - unique ID of the row
  • "Store_ID" - the store number
  • "Date" - the week of sales
  • "Weekly_Sales" - sales for the given store

Also, you have the extra_data.parquet file that contains complementary data:

extra_data.parquet

  • "IsHoliday" - Whether the week contains a public holiday - 1 if yes, 0 if no.
  • "Temperature" - Temperature on the day of sale
  • "Fuel_Price" - Cost of fuel in the region
  • "CPI" – Prevailing consumer price index
  • "Unemployment" - The prevailing unemployment rate
  • "MarkDown1", "MarkDown2", "MarkDown3", "MarkDown4" - number of promotional markdowns
  • "Dept" - Department Number in each store
  • "Size" - size of the store
  • "Type" - type of the store (depends on Size column)

You will need to merge those files and perform some data manipulations. The transformed DataFrame can then be stored as the clean_data variable containing the following columns:

  • "Store_ID"
  • "Month"
  • "Dept"
  • "IsHoliday"
  • "Weekly_Sales"
  • "CPI"
  • ""Unemployment""

After merging and cleaning the data, you will have to analyze monthly sales of Walmart and store the results of your analysis as the agg_data variable that should look like:

MonthWeekly_Sales
1.033174.178494
2.034333.326579
......

Finally, you should save the clean_data and agg_data as the csv files.

It is recommended to use pandas for this project.

Spinner
DataFrameas
grocery_sales
variable
SELECT * FROM grocery_sales
Hidden output
import pandas as pd
import numpy as np
import os

def extract(store_data, extra_data):
    extra_df = pd.read_parquet(extra_data)
    merged_df = store_data.merge(extra_df, on = "index")
    return merged_df
    
def transform(raw_data):
    raw_data= raw_data.set_index('index', drop=True)

    # Fill missing data
    raw_data=raw_data.fillna({
        'Dept': raw_data['Dept'].mode(),
        'Weekly_Sales': raw_data['Weekly_Sales'].mean(),
        'IsHoliday': raw_data['IsHoliday'].mode(),
        'Temperature': raw_data['Temperature'].mean(),
        'Fuel_Price': raw_data['Fuel_Price'].mean(),
        'MarkDown1': raw_data['MarkDown1'].mean(),
        'MarkDown2': raw_data['MarkDown2'].mean(),
        'MarkDown3': raw_data['MarkDown3'].mean(),
        'MarkDown4': raw_data['MarkDown4'].mean(),
        'MarkDown5': raw_data['MarkDown5'].mean(),
        'CPI': raw_data['CPI'].mean(),
        'Unemployment' :raw_data['Unemployment'].mean(),
        'Type': raw_data['Type'].mean(),
        'Size': raw_data['Size'].mean()
    })
    raw_data['Date'] = raw_data['Date'].fillna(method='ffill')
    assert raw_data.isna().any().any()==False

    # Create new features, and subset
    raw_data['Month'] = raw_data['Date'].dt.month 
    raw_data=raw_data.loc[raw_data['Weekly_Sales']>10000,:]
    desired_cols = ["Store_ID", "Month", "Dept", "IsHoliday", 
                    "Weekly_Sales","CPI", "Unemployment"]
    raw_data=raw_data[desired_cols]

    # Create table of aggregated data
    agg_data = raw_data[['Month','Weekly_Sales']].groupby('Month', as_index=False).mean().round(2)
    agg_data = agg_data.rename(columns={'Weekly_Sales':'Avg_Sales'})
    
    return raw_data, agg_data

def load(full_data, full_data_file_path, agg_data, agg_data_file_path):

    full_data.to_csv(full_data_file_path, index=False)
    agg_data.to_csv(agg_data_file_path, index=False)
    return None
    
def validation(file_path):
    # check whether the two csv files from load exist in the 
    assert os.path.exists(file_path), f"Validation failed, no file exists at {file_path}"
    return

def pipeline(clean_fp, agg_fp):
    
    raw_df = extract(grocery_sales, "extra_data.parquet")
    clean_data, agg_data = transform(raw_df)
    load(clean_data, clean_fp, agg_data,agg_fp)
    
    validation(clean_fp)
    validation(agg_fp)

if __name__ =="__main__":
    pipeline('clean_data.csv', 'agg_data.csv')