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
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
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 onSize
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:
Month | Weekly_Sales |
---|---|
1.0 | 33174.178494 |
2.0 | 34333.326579 |
... | ... |
Finally, you should save the clean_data
and agg_data
as the csv files.
It is recommended to use pandas
for this project.
-- Write your SQL query here
SELECT *
FROM grocery_sales
grocery_sales.info()
import pandas as pd
import numpy as np
import logging
import os
# Start here...
# extra_data
extra_data = pd.read_parquet('extra_data.parquet')
extra_data.info()
# Extract function
def extract(df1: pd.DataFrame = None, df2: pd.DataFrame = None) -> pd.DataFrame:
"""
Summary:
Arguments:
Returns:
"""
try:
df = df1.merge(right = df2, how = 'inner', on = 'index', validate = '1:1')
return df
except Exception as ex:
print(ex)
# Merge DataFrame
merged_df = extract(df1 = grocery_sales, df2 = extra_data)
merged_df.info()
# Transform function
def transform(df: pd.DataFrame = None) -> pd.DataFrame:
"""
Summary:
Arguments:
Returns:
"""
try:
df = df \
.fillna({"Weekly_Sales": df.Weekly_Sales.mean(),
"CPI": df.CPI.mean(),
"Unemployment": df.Unemployment.mean()}) \
.assign(Month = df['Date'].dt.month) \
.loc[:, ["Store_ID", "Month", "Dept", "IsHoliday",
"Weekly_Sales", "CPI", "Unemployment"]] \
.iloc[lambda x: list(x.Weekly_Sales > 10000), :]
return df
except Exception as ex:
print(ex)
clean_data = transform(merged_df)
clean_data.head()
# avg_monthly_sales function
def avg_monthly_sales(df: pd.DataFrame = None) -> pd.DataFrame:
"""
Summary:
Arguments:
Returns:
"""
try:
df = df.groupby(by = 'Month', as_index = False) \
.agg(Avg_Sales = pd.NamedAgg('Weekly_Sales', np.mean)) \
.round(2)
return df
except Exception as ex:
print(ex)
agg_data = avg_monthly_sales(df = clean_data)
agg_data
# Load function
def load(clean_data_df: pd.DataFrame = None, clean_data_df_filepath: str = None,
agg_data_df: pd.DataFrame = None, agg_data_df_filepath: str = None):
"""
Summary:
Parameters:
Returns
"""
try:
clean_data_df.to_csv(clean_data_df_filepath, index = False)
agg_data_df.to_csv( agg_data_df_filepath, index = False)
except Exception as ex:
print(ex)
load(clean_data_df = clean_data, clean_data_df_filepath = 'clean_data.csv',
agg_data_df = agg_data, agg_data_df_filepath = 'agg_data.csv')
# validation function
def validation(filepath: str = None):
"""
Summary:
Parameters:
Returns:
"""
if os.path.exists(filepath):
print('File exists')
else:
raise Exception('File doesnt exists')
validation('clean_data.csv')