Pre-Requisites to Understanding this Article
What's a Dictionary in Python?
How are dictionaries useful for us as Data Professionals?
Retail Sales Analysis 🛍️
Step One - Import your Data
import pandas as pd
import numpy as np
dataframe = pd.read_csv("/work/City_Sales_Data.csv")
dataframe.head()
Step Two - Use Dictionaries to Map our Aliases
import plotly.express as px
location_dict = {
"A": "Los Angeles",
"B": "Fresno",
"C": "San Jose"
}
dataframe['Location_Mapped'] = dataframe['Location'].map(location_dict)
data = dataframe.groupby(['Location_Mapped','Product line'])['Total'].agg(['sum', 'mean']).reset_index()
data.columns = ['Location_Mapped','Product_Line','Total_Sales','Average_Sales']
data['Total_Sales'] = round(data['Total_Sales'])
fig = px.bar(data, x="Location_Mapped", y="Total_Sales", color="Product_Line", text="Total_Sales", title="Stacked Column Chart - Sales by Location and Product")
fig.show()
data = dataframe.groupby(['Location_Mapped','Gender','Customer type']).sum()['Total'].reset_index()
data['Total'] = round(data['Total'])
fig = px.bar(data, x="Location_Mapped", y="Total", color="Gender", facet_row="Customer type", text="Total",title="Stacked Column Chart - Sales by Location and Gender")
fig.show()
#Convert the Object to a Datetime Object
dataframe['Date']= pd.to_datetime(dataframe['Date'], dayfirst=True)
#Create Month Column from Datetime Object ensuring Dayfirst = True
dataframe['month'] = dataframe['Date'].dt.month
dict_month = {
1: "Jan-19",
2: "Feb-19",
3: "Mar-19"
}
dataframe['month'] = dataframe['month'].map(dict_month)
data = dataframe.groupby(['month','Product line']).sum()['Total'].reset_index()
data['Total'] = round(data['Total'])
fig = px.bar(data, x="month", y="Total", color="Product line", text="Total", title="Stacked Column Chart - Sales by Month and Product", category_orders=['Jan-19','Feb-19','Mar-19'])
fig.show()