├── py report.pdf └── retail_dashboard.py /py report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gauravtiwarrii/Python-Project/34b94bb9aa53df293f0afb9594b30dc24e3dec31/py report.pdf -------------------------------------------------------------------------------- /retail_dashboard.py: -------------------------------------------------------------------------------- 1 | import dash 2 | from dash import dcc, html 3 | import dash_mantine_components as dmc 4 | import pandas as pd 5 | import plotly.express as px 6 | import plotly.figure_factory as ff 7 | 8 | # Load dataset 9 | df = pd.read_excel("online_retail_II.xlsx", sheet_name="Year 2009-2010") # Adjust if multiple sheets! 10 | 11 | # Preprocessing 12 | df = df.dropna(subset=["Customer ID"]) # Remove missing customers 13 | df["InvoiceDate"] = pd.to_datetime(df["InvoiceDate"]) 14 | df["Month"] = df["InvoiceDate"].dt.to_period("M").dt.to_timestamp().astype(str) # Convert to string for JSON serialization 15 | 16 | # Figures 17 | # 1. Demographics - Customers per country 18 | fig_demographics = px.bar( 19 | df["Country"].value_counts().head(10), 20 | labels={"value": "Number of Customers", "index": "Country"}, 21 | title="Top 10 Countries by Number of Customers" 22 | ) 23 | 24 | # 2. Trends - Monthly revenue 25 | monthly_revenue = df.groupby(df["InvoiceDate"].dt.to_period("M")).sum(numeric_only=True)["Price"] 26 | fig_trends = px.line( 27 | x=monthly_revenue.index.astype(str), 28 | y=monthly_revenue, 29 | title="Monthly Revenue Trends", 30 | labels={"y": "Revenue", "x": "Month"} 31 | ) 32 | 33 | # 3. Customer Segmentation - RFM Analysis (example) 34 | rfm = df.groupby("Customer ID").agg({ 35 | "InvoiceDate": lambda x: (df["InvoiceDate"].max() - x.max()).days, 36 | "Invoice": "nunique", 37 | "Price": "sum" 38 | }).rename(columns={"InvoiceDate": "Recency", "Invoice": "Frequency", "Price": "MonetaryValue"}) 39 | fig_segmentation = px.scatter( 40 | rfm, x="Recency", y="MonetaryValue", size="Frequency", 41 | title="Customer Segmentation (RFM)", 42 | hover_name=rfm.index 43 | ) 44 | 45 | # 4. Seasonality - Sales by Month 46 | fig_seasonality = px.bar( 47 | df.groupby(df["InvoiceDate"].dt.month).sum(numeric_only=True)["Price"], 48 | labels={"value": "Sales", "InvoiceDate": "Month"}, 49 | title="Sales Seasonality by Month" 50 | ) 51 | 52 | # 5. Returns Insight - Returns by Country (Improved) 53 | returns = df[df["Quantity"] < 0] 54 | returns_by_country = returns["Country"].value_counts() 55 | # Group countries with less than 2% into "Others" 56 | threshold = returns_by_country.sum() * 0.02 57 | top_countries = returns_by_country[returns_by_country >= threshold] 58 | others = returns_by_country[returns_by_country < threshold].sum() 59 | returns_by_country_adjusted = pd.concat([ 60 | top_countries, 61 | pd.Series(others, index=["Others"]) 62 | ]) 63 | fig_returns = px.pie( 64 | returns_by_country_adjusted, 65 | names=returns_by_country_adjusted.index, 66 | values=returns_by_country_adjusted, 67 | title="Returns Distribution by Country", 68 | hole=0.3, # Add a donut hole for better aesthetics 69 | ) 70 | 71 | # 6. Correlation Heatmap 72 | # Select numerical columns for correlation 73 | numerical_cols = ["Quantity", "Price", "Customer ID"] 74 | corr_matrix = df[numerical_cols].corr() 75 | # Create annotated heatmap with a visible colorscale 76 | fig_correlation = ff.create_annotated_heatmap( 77 | z=corr_matrix.values, 78 | x=numerical_cols, 79 | y=numerical_cols, 80 | colorscale="Viridis", # Changed to Viridis for better visibility 81 | showscale=True, 82 | zmin=-1, 83 | zmax=1, 84 | annotation_text=corr_matrix.round(2).values, 85 | textfont={"size": 12, "color": "black"}, # Ensure text is visible 86 | hoverinfo="z" 87 | ) 88 | fig_correlation.update_layout( 89 | title="Correlation Heatmap of Numerical Features", 90 | height=400, # Adjust height for better fit 91 | margin=dict(l=50, r=50, t=100, b=50), # Adjust margins 92 | ) 93 | 94 | # Dash App 95 | app = dash.Dash(__name__) 96 | 97 | app.layout = dmc.MantineProvider( 98 | inherit=True, 99 | theme={ 100 | "colorScheme": "dark", 101 | "primaryColor": "teal", # Change tab color to teal 102 | "colors": { 103 | "teal": ["#E6FFFA", "#B2F5EA", "#81E6D9", "#4FD1C5", "#38B2AC", "#319795", "#2C7A7B", "#285E61", "#233E5F", "#1A2E44"] 104 | } 105 | }, 106 | children=[ 107 | dmc.Container( 108 | [ 109 | dmc.Title("🛍️ Decoding Retail Dynamics Dashboard", order=2, align="center", mb=20), 110 | dmc.Text("An interactive dashboard for analyzing retail customer behavior, trends, and returns.", align="center", size="md", color="dimmed"), 111 | dmc.Tabs( 112 | value="tab1", 113 | children=[ 114 | dmc.TabsList( 115 | [ 116 | dmc.Tab("Demographics", value="tab1"), 117 | dmc.Tab("Trends", value="tab2"), 118 | dmc.Tab("Customer Segmentation", value="tab3"), 119 | dmc.Tab("Seasonality", value="tab4"), 120 | dmc.Tab("Returns Insight", value="tab5"), 121 | dmc.Tab("Correlation", value="tab6"), 122 | ], 123 | grow=True, 124 | mb=20, 125 | ), 126 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_demographics), type="default"), value="tab1"), 127 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_trends), type="default"), value="tab2"), 128 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_segmentation), type="default"), value="tab3"), 129 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_seasonality), type="default"), value="tab4"), 130 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_returns), type="default"), value="tab5"), 131 | dmc.TabsPanel(dcc.Loading(dcc.Graph(figure=fig_correlation), type="default"), value="tab6"), 132 | ], 133 | ), 134 | ], 135 | style={"padding": "2rem"}, 136 | ) 137 | ] 138 | ) 139 | 140 | if __name__ == "__main__": 141 | app.run(debug=True) 142 | --------------------------------------------------------------------------------