Exploratory data analysis using supermarket sales data in Python
By: Mythili Krishnan
Tags: Exploratory Data Analysis, Correlation, Python, Multivariate Analysis

Ans: Gross income is highest in sports and travel.

Pairwise plot: We can look at all the bi-variate relationships in the data using pairwise plot in one go. But it is recommended not to use this method in large datasets because it provides all possible variable combinations. A snippet of our data today is given below:

3. Correlation analysis

Correlation is the degree to which a pair of variables is linearly related.

Correlation analysis indicates the positive or negative or zero correlation between the variables. A positive correlation means that the values of the 2 variables increase together and negative correlation means the values of one variable decrease with the other.Zero correlation implies no correlation.

The seaborn heat-map can be used to visualize the correlations among variables.

sns.heatmap(np.round(df.corr(),2), annot=True)

Fig 10: Correlation analysis (Image by Author)

As you can see, unit price is positively correlated to cogs with 63% correlation. Another interesting observation is that ‘Ratings’ hardly has any correlation with any other variables.

As is obvious, Quantity and gross income has very high correlation of 70%.

4. Other analysis

Question 9: What is the spending pattern of females and males and in which category do they spend a lot?

For the analysis on spending patterns of males and females, we can create create a dummy variable for Gender and concatenate it with the original data.(You can find the code for this here).

We can also utilize the ‘hue’ option to get this information in one chart.

plt.figure(figsize=(12, 6))

plt.title('Total Monthly transaction by Gender')

sns.countplot(df['Product line'], hue = df.Gender)

Fig 11: Spending pattern: Males vs Female (Image by author)

Ans: Females spend on 'fashion accessories' the most and for males it is 'Health and beauty'. Females also spend more on 'Sports and travel'.

Question 10: How many products are bought by customers?

Using distribution plot and heat map we can explore the number of products/quantities that most customers buy.

xdata = [1,2,3,4,5,6,7,8,9,10]

plt.figure(figsize = (12,6))

sns.distplot(df['Quantity'])

plt.xticks(xdata)

Fig 12: Quantity distribution Plot (Image by author)

Ans: Most of the customers buy 10 quantities.

Question 11: Which day of the week has maximum sales?

Let us now explore if there is any particular day of the week when the sales is higher.

plt.figure(figsize=(8, 6))

plt.title('Daily Sales by Day of the Week')

sns.countplot(df['weekday'])

Fig 13: Daily sales by day of the week (Image by Author)

Ans: Sales is highest on Saturdays probably because it is the weekend. Interestingly,Tuesdays is a close second.Mondays is the lowest in sales, probably because it is start of the working week.

Question 12: Which hour of the day is the busiest?

We need to extract the hour from the ‘Date’ variable to do this analysis. This can be done in the following way.

df['Time'] = pd.to_datetime(df['Time'])

df['Hour'] = (df['Time']).dt.hour

df['Hour'].unique()

Plotting the data we get the below output:

sns.lineplot(x="Hour", y = 'Quantity',data =df).set_title("Product Sales per Hour")

Fig 14: Hourly sales of products (Image by author)

Ans:Peak is observed in the 14th hour i.e 2 pm of the day. Hence, sales is typically higher in the afternoons.

Question 13: Which product line should the supermarket focus on?

To answer this question, let us look at 2 graphs below:

a) Rating of products: Rating is similar across products and at around 7.

xdata = [0,1,2,3,4,5,6,7,8,9,10]

plt.figure(figsize = (12,6))

sns.barplot(y = df['Product line'], x = df['Rating'])

plt.xticks(xdata)

Fig 15: Ratings across product lines (Image by Author)

b) Quantity purchased by product: The mean quantity is lower for ‘fashion accessories’ and ‘food and beverages’

sns.boxenplot(y = 'Product line', x = 'Quantity', data=df )

Fig 16: Quantity of products (Image by Author)

Ans: Though the rating for ‘fashion accessories’ and ‘food and beverages’ is high but quantity purchased is low. Hence, supply for these products need to be increased.

Question 14: Which city should be chosen for expansion and which products should it focus on?

Let us first answer which city should be chosen for expansion:

plt.figure(figsize=(20,7))

sns.barplot(df['City'],df['gross income'],palette='Set1')

plt.xlabel('City name',fontsize='16')

plt.xticks(fontsize='16')

plt.ylabel('Gross income',fontsize='16')

plt.yticks(fontsize='16')

Fig 17: Gross income across branches/cities: (Image by Author)

Ans: It is obvious that Naypyitaw is the most profitable city, hence the expansion plan should be based on this city.



Let us look at the products sold at ‘Naypyitaw’

plt.figure(dpi=125)

sns.countplot(y ='Product line', hue = "City", data = df)

plt.xlabel('Count')

plt.show()

Fig 18: Sales of products across cities/branches

Ans: Fashion accessories and food and beverages are the most sold product in Naypyitaw and these products should be focused on for expansion along with electronic accessories.

Conclusion:

We used uni-variate, bi-variate and correlation analysis to perform basic EDA on the supermarket sales data.

To summarize below are some of the findings/observations from the data:

The customer rating is more or less uniform with the mean rating being around 7 and there is no relationship between gross income and customer ratings. The data consists of 3 cities/branches. Though branch A has slightly higher sales than the rest, C i.e. Naypyitaw is the most profitable branch in terms of gross income. Fashion accessories and food and beverages are the most sold product in Naypyitaw and these products should be focused on along with electronic accessories. The most popular payment method is E-wallet and cash payment is also on the higher side. There is no particular time trend that can be observed in gross income. At an overall level, ‘Sports and Travel’ generates highest gross income. Gross income is similar for both male and female, though female customers spend a bit higher at the 75th percentile. Females spend on ‘fashion accessories’ the most and for males surprisingly it is ‘Health and beauty’. Females also spend more on ‘Sports and travel’ which generates highest income overall. Using the correlation analysis, one interesting observation has emerged that customer ratings is not related to any variable. Most of the customers buy 10 quantities and busiest time of the day is afternoon i.e. around 2 pm which records highest sales. Sales is higher on Tuesdays and Saturdays compared to the rest of the week. Though the rating for ‘fashion accessories’ and ‘food and beverages’ is high but the quantity purchased is low. Hence, supply for these products need to be increased.

All the analysis can be accessed here on Github.

I can be reached on medium, LinkedIn or Twitter in case of any questions/comments.

References:

[1] Suresh Kumar Mukhiya, ‎Usman Ahmed, Hands on Exploratory data analysis with Python(2020)

[2] Meni Koslowsky, Univariate and Multivariate Analysis of Categorical Variables, (1979), Springer

[3] Shaun McQuitty, The Purposes of Multivariate Data Analysis Methods: an Applied Commentary (13 Sep 2017), Journal of African Business

[4] Chris Chatfield, Exploratory data analysis,(Jan 1986), European Journal of Operational Research,Volume 23, Issue 1