{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "3be52891", "metadata": {}, "outputs": [], "source": [ "import pandas as pd \n", "import numpy as np # For mathematical calculations \n", "import seaborn as sns # For data visualization \n", "import matplotlib.pyplot as plt # For plotting graphs \n", "import warnings # To ignore any warnings \n", "warnings.filterwarnings(\"ignore\")\n", "pd.options.display.float_format = '{:.2f}'.format\n", "import plotly.express as px\n", "from plotly.subplots import make_subplots\n", "import plotly.graph_objects as go" ] }, { "cell_type": "code", "execution_count": null, "id": "e503b0f8", "metadata": {}, "outputs": [], "source": [ "# This example is from Kaggle: https://www.kaggle.com/brijbhushannanda1979/bigmart-sales-data/version/1?select=Train.csv\n", "data = pd.read_csv('Train.csv')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "ee5e8e0a", "metadata": {}, "outputs": [], "source": [ "table = pd.DataFrame(data.groupby(['Outlet_Size','Outlet_Location_Type','Outlet_Type'],as_index = False)['Item_Outlet_Sales'].sum()).sort_values('Item_Outlet_Sales',ascending=False)\n", "cm = sns.light_palette(\"seagreen\", as_cmap=True)\n", "table.style.background_gradient(cmap=cm)\n", "\n", "#total sales\n", "\n", "plt.figure(figsize=(8,5))\n", "type2=data.groupby(['Outlet_Type'])['Item_Outlet_Sales'].sum()\n", "store_types=['Grocery Store', 'Supermarket Type1', 'Supermarket Type2', 'Supermarket Type3']\n", "plot1 = sns.barplot(store_types,type2, color='coral')\n", "plt.ticklabel_format(axis=\"y\", style=\"plain\")\n", "plt.xlabel('Outlet Type')\n", "plt.ylabel('Item Outlet Sales')\n", "ylabels = ['{:,.0f}'.format(y) + 'M USD' for y in plot1.get_yticks()/1000000]\n", "plot1.set_yticklabels(ylabels)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "63eaff31", "metadata": {}, "outputs": [], "source": [ "#price\n", "plt.rcParams['figure.figsize'] = 16,4\n", "plot2 =sns.boxplot(x=\"Item_Type\",y=\"Item_MRP\",data=data,palette='husl')\n", "plot2.set_xticklabels(plot2.get_xticklabels(), rotation=15,horizontalalignment='right', fontweight='light',fontsize='x-large')\n", "ylabels = ['{:,.0f}'.format(y) + ' USD' for y in plot2.get_yticks()]\n", "plot2.set_yticklabels(ylabels)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "0ffe1bfb", "metadata": {}, "outputs": [], "source": [ "#outlet types\n", "plt.figure(figsize=(8,5))\n", "plot3 = sns.countplot('Outlet_Type',data=data,palette='autumn')\n", "plt.xlabel('Outlet_Type')\n", "plt.ylabel('Count')\n", "ylabels = ['{:,.0f}'.format(y) + ' stores' for y in plot3.get_yticks()]\n", "plot3.set_yticklabels(ylabels)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9b88418a", "metadata": {}, "outputs": [], "source": [ "#item breakdown\n", "df3=data.groupby(by='Item_Type').sum()\n", "df2=df3['Item_Outlet_Sales'].sort_values(ascending=False)\n", "plot4 = px.pie(df2, values='Item_Outlet_Sales', names=['Fruits and Vegetables', 'Snack Foods','Household ','Frozen Foods','Dairy ', 'Canned','Baking Goods','Health and Hygiene','Meat', 'Soft Drinks','Breads','Hard Drinks','Starchy Foods', 'Others','Breakfast','Seafood'])\n", "plot4.layout.update(showlegend=False)\n", "plot4.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "913a0992", "metadata": {}, "outputs": [], "source": [ "#sales per year\n", "y = data.groupby(['Outlet_Establishment_Year']).sum()\n", "y = y['Item_Outlet_Sales']\n", "x = y.index.astype(int)\n", "\n", "plt.figure(figsize=(16,4))\n", "plot5 = sns.barplot(y = y, x = x, palette='summer')\n", "ax2 = plot5.twinx()\n", "ax2.plot(plot5.get_xticks(), y, marker = 'o', color='red', linewidth=2.5)\n", "ylabels_ax2 = ['{:,.0f}'.format(y) + 'M USD' for y in ax2.get_yticks()/1000000]\n", "ax2.set_yticklabels(ylabels_ax2)\n", "plot5.set_xlabel(xlabel='Year', fontsize=16)\n", "plot5.set_xticklabels(labels = x, fontsize=12, rotation=50)\n", "plot5.set_ylabel(ylabel='Sales', fontsize=16)\n", "plot5.set_title(label='Sales Per Year', fontsize=20)\n", "ylabels = ['{:,.0f}'.format(y) + 'M USD' for y in plot5.get_yticks()/1000000]\n", "plot5.set_yticklabels(ylabels)\n", "plt.show();" ] }, { "cell_type": "code", "execution_count": null, "id": "de7b5455", "metadata": {}, "outputs": [], "source": [ "#fat content\n", "data['Item_Fat_Content'].replace({'reg':'Regular','low fat':'Low Fat','LF':'Low Fat'},inplace = True)\n", "\n", "\n", "df4=data.groupby(by='Item_Fat_Content').sum()\n", "df5=df4['Item_Outlet_Sales'].sort_values(ascending=False)\n", "plot6 = px.pie(df5, values='Item_Outlet_Sales',names= ['Low Fat','Regular'])\n", "plot6.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9c701ba5", "metadata": {}, "outputs": [], "source": [ "# # Datapane work\n", "\n", "import datapane as dp\n", "\n", "commentary = 'Supermarket type1 has contributed highest to the overall sales, this can be attributed to the fact that Type1 supermarkets high in numbers and dominate every Tier. However, the business has seen a rapid decline in sales since its launch. In terms of the products being sold, items with low fat content are being sold more due to customer preferences.'\n", "\n", "# Enter Heading information\n", "\n", "header_image = \"\"\"\n", "\n", " \n", " \n", "
\n", "
\n", "\n", " \n", " ABC
\n", " Company\n", "\n", " \n", " 15 JUL 2021 \n", "\n", " \n", "

SALES REPORT

\n", "\n", " \n", "

This report shows the single pager for the company's sales report. It covers how stores are performing and what all items are selling. Using interactive plots, it all shows how sales have changed over the years.

\n", "
\n", "
\n", "\n", "\n", "\"\"\"\n", "\n", "\n", "# Page layout \n", "\n", "report = dp.TextReport(\n", " dp.HTML(header_image, label=\"Header image\"),\n", " dp.Table(table.style.background_gradient(cmap=cm), label = \"Outlet performance\"),\n", " dp.Plot(plot5, label = \"Sales per year\"),\n", " dp.Plot(plot1),\n", " dp.Plot(plot3),\n", " dp.Plot(plot2),\n", " dp.Plot(plot4),\n", " dp.Group(\n", " dp.BigNumber(\n", " heading=\"Low Fat sales percentage\", \n", " value=\"64%\",\n", " change=\"10%\",\n", " is_upward_change=True),\n", " dp.BigNumber(\n", " heading=\"Regular Fat sales percentage\", \n", " value=\"36%\",\n", " change=\"5%\",\n", " is_upward_change=False),\n", " columns = 2),\n", ").upload(name='Sales report template', open=True)\n", "#If you have an existing TextReport, then use the following code instead, replacing 'foo' with your Text Report ID \n", "# ).upload(id='foo', open=True)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }