<jupyter_start><jupyter_text>Source: https://www.kaggle.com/aungpyaeap/supermarket-salesContext:The growth of supermarkets in most populated cities are increasing and market competitions are also high. The dataset is one of the historical sales of supermarket company which has recorded in 3 different branches for 3 months data.  Attribute informationInvoice id: Computer generated sales slip invoice identification numberBranch: Branch of supercenter (3 branches are available identified by A, B and C).City: Location of supercentersCustomer type: Type of customers, recorded by Members for customers using member card and Normal for without member card.Gender: Gender type of customerProduct line: General item categorization groups - Electronic accessories, Fashion accessories, Food and beverages, Health and beauty, Home and lifestyle, Sports and travelUnit price: Price of each product in $Quantity: Number of products purchased by customerTax: 5% tax fee for customer buyingTotal: Total price including taxDate: Date of purchase (Record available from January 2019 to March 2019)Time: Purchase time (10am to 9pm)Payment: Payment used by customer for purchase (3 methods are available – Cash, Credit card and Ewallet)COGS: Cost of goods soldGross margin percentage: Gross margin percentageGross income: Gross incomeRating: Customer stratification rating on their overall shopping experience (On a scale of 1 to 10)  Objective: DATA ANALYSIS  Import libraries<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as plt
%matplotlib inline
from datetime import datetime
import matplotlib.pyplot as plot<jupyter_output><empty_output><jupyter_text>Import dataset<jupyter_code>df=pd.read_csv('supermarket_sales.csv')<jupyter_output><empty_output><jupyter_text>Feature engineering<jupyter_code># Dataset dimensionality
df.shape
# Data type of each column
df.dtypes
df = df.set_index('Invoice ID')
# Working in the DATE column
df['date'] = pd.to_datetime(df['Date'])
df['date'].dtype
df['date'] = pd.to_datetime(df['date'])
df['day'] = (df['date']).dt.day
df['month'] = (df['date']).dt.month
df['year'] = (df['date']).dt.year
del(df["date"])
# Working in the TIME column
df['Time'] = pd.to_datetime(df['Time'])
df['Hour'] = (df['Time']).dt.hour
df['Hour'].unique()
df.drop(['Tax 5%','Total','cogs','Branch','Date','Time'], inplace=True, axis=1)
# Showing the categorical columns
categorical_columns = [cname for cname in df.columns if df[cname].dtype == "object"]
categorical_columns
# There isn't data missing
df.isna().sum()
df.head(10)
sum(df['gross income'])
df.describe()
df['month'].unique() 
df['Hour'].unique()
df['day'].unique()<jupyter_output><empty_output><jupyter_text>Data visualization<jupyter_code>df_gross_income = df[['Product line', 'gross income']].groupby(['Product line'], as_index = False).sum()
df_gross_income['percent'] = (df_gross_income['gross income'] / df_gross_income['gross income'].sum()) * 100
df_gross_income = pd.DataFrame(df_gross_income)
df_gross_income.sort_values(by=['percent'],ascending = False)

# Below we can see that there is no product line that gives much more results than the others.
df_city = df[['City', 'gross income']].groupby(['City'], as_index = False).sum()
df_city['percent'] = (df_city['gross income'] / df_city['gross income'].sum()) *100
df_city = pd.DataFrame(df_city)
df_city.sort_values(by=['percent'], ascending = False)

# There isn't city that is significantly relevant
df_product = df[['Product line', 'Gender', 'Quantity']]
df_product = df_product.groupby(['Product line', 'Gender'], as_index = False).sum()
df_product
df_revenue_city = df.groupby('City').agg([np.sum, np.median, np.mean])
df_revenue_city[['gross income']].T
# Quantity sales by city
sns.countplot(x="City", data=df)
# Quantidade de vendas por linha de produto
sns.countplot(y="Product line", data=df)
sns.catplot(x='Product line',y='gross income',hue='Gender',data=df, kind = 'bar', aspect = 3 )
# Quantidade de vendas por gênero
sns.countplot(x='Gender', data=df)
# Quantidade de vendas por tipo de pagamento
sns.countplot(x='Payment', data=df)
# Vendas por mês
sns.barplot(x="month", y="gross income", hue="Product line", data=df)
# Vendas por hora do dia
sns.barplot(x='Hour',y='gross income',data=df)
sns.catplot(y="Product line", x="Quantity", kind="box", data=df)
df_corr = df[['Unit price', 'Quantity', 'gross margin percentage', 'gross income', 'Rating']]
np.round(df_corr.corr(method = 'pearson'),5)
correlations = df_corr.corr()
colunas1 = ['Unit price', 'Quantity', 'gross margin percentage', 'gross income', 'Rating']
colunas2 = ['UP', 'Qtity', 'GM%', 'GI', 'Rating']

# Plot
fig = plot.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(correlations, vmin = -1, vmax = 1)
fig.colorbar(cax)
ticks = np.arange(0, 5, 1)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels(colunas2)
ax.set_yticklabels(colunas1)
plot.show()
df_corr.skew()
sns.lineplot(x='day',y='gross income', data=df)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13812375249501, 'fraction_numerical': 0.008582834331337325, 'mean_word_length': 6.916271721958926, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '259765', 'license': 'MIT', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1610, 'n_words': 484, 'path': 'Data_Analysis_Supermarket_Sales.ipynb', 'repo_name': 'fernandoboera/supermarket_sales.data_analysis', 'size': '5010'}