I want to utilize Python to apply EDA. The required steps are highlighted as follows:
Import libraries including numpy and pandas.
Import SampleSuperstore dataset (xlsx format) as a pandas dataframe, call it, df.
View the data frame (implicity, view the dataset).
View the shape of the dataset and also display the name of columns.
View information about the dataset including range index, datatypes, number of non-nulls entries. Use info() method.
View unique categories.
View the unique list of states.
View the unique categories and subcategories.
View the statistical description of the dataframe.
Create a new dataframe where the profit is negative, i.e., loss and call is loss_df
In the loss_df, group by “Segment”. What is your observation?
Repeat step 11, this time grouping by sub_category. What is your observation?
Rank the Sub-Categories by the number of records in the loss_df. Use the value_counts() method. What do you observe?
In the loss_df, group by “City” and sort values by profit. What do you observe?
In the df, group by “Region and display Sales, Quantity, Discount, and Profit.
Provide me the code to implement these steps properly. This is a sample of the data in the excel file including the headers and first row of values. Row ID Order ID Order Date Ship Date Ship Mode Customer ID Customer Name Segment Country City State Postal Code Region Product ID Category Sub-Category Product Name Sales Quantity Discount Profit
1 CA-2016-152156 2016-11-08 2016-11-11 Second Class CG-12520 Claire Gute Consumer United States Henderson Kentucky 42420 South FUR-BO-10001798 Furniture Bookcases Bush Somerset Collection Bookcase 261.96 2 0 41.9136
Assistant
Here is the code to implement the steps you've outlined:
python
# Step 1: Import libraries including numpy and pandas
import numpy as np
import pandas as pd
# Step 2: Import SampleSuperstore dataset (xlsx format) as a pandas dataframe, call it df
df = pd.read_excel('SampleSuperstore.xlsx')
# Step 3: View the data frame (implicitly, view the dataset)
print("First five rows of the dataset:")
print(df.head())
# Step 4: View the shape of the dataset and also display the name of columns
print("\nShape of the dataset:", df.shape)
print("Column names:", df.columns.tolist())
# Step 5: View information about the dataset including range index, datatypes, number of non-nulls entries. Use info() method.
print("\nDataset information:")
df.info()
# Step 6: View unique categories
print("\nUnique categories:")
print(df['Category'].unique())
# Step 7: View the unique list of states
print("\nUnique states:")
print(df['State'].unique())
# Step 8: View the unique categories and subcategories
print("\nUnique categories and sub-categories:")
print("Categories:", df['Category'].unique())
print("Sub-Categories:", df['Sub-Category'].unique())
# Step 9: View the statistical description of the dataframe
print("\nStatistical description of the dataframe:")
print(df.describe())
# Step 10: Create a new dataframe where the profit is negative, i.e., loss and call it loss_df
loss_df = df[df['Profit'] < 0]
# Step 11: In the loss_df, group by “Segment”. What is your observation?
segment_loss = loss_df.groupby('Segment')['Profit'].sum()
print("\nTotal loss by Segment:")
print(segment_loss)
# Observation:
# We can observe which customer segments are contributing the most to the losses.
# Step 12: Repeat step 11, this time grouping by sub_category. What is your observation?
subcategory_loss = loss_df.groupby('Sub-Category')['Profit'].sum()
print("\nTotal loss by Sub-Category:")
print(subcategory_loss.sort_values())
# Observation:
# Identifies sub-categories with the highest losses, indicating areas needing attention.
# Step 13: Rank the Sub-Categories by the number of records in the loss_df. Use the value_counts() method. What do you observe?
subcategory_counts = loss_df['Sub-Category'].value_counts()
print("\nSub-Categories ranked by number of loss-making records:")
print(subcategory_counts)
# Observation:
# Shows which sub-categories frequently result in losses.
# Step 14: In the loss_df, group by “City” and sort values by profit. What do you observe?
city_loss = loss_df.groupby('City')['Profit'].sum().sort_values()
print("\nCities with the highest total losses:")
print(city_loss.head(10)) # Display top 10 cities with the highest losses
# Observation:
# Highlights cities where losses are the greatest.
# Step 15: In the df, group by “Region” and display Sales, Quantity, Discount, and Profit.
region_stats = df.groupby('Region')[['Sales', 'Quantity', 'Discount', 'Profit']].sum()
print("\nSales, Quantity, Discount, and Profit by Region:")
print(region_stats)
Explanation and Observations:
Step 1: Imports the necessary libraries for data manipulation.
Step 2: Reads the Excel file into a DataFrame called df.
Step 3: Displays the first five rows to get an initial look at the data.
Step 4: Shows the dimensions of the dataset and lists all column names.
Step 5: Provides detailed information about the DataFrame, including data types and non-null counts.
Step 6: Lists all unique categories in the 'Category' column.
Step 7: Lists all unique states represented in the dataset.
Step 8: Displays unique categories and sub-categories to understand the product hierarchy.
Step 9: Presents statistical measures like mean, standard deviation, and quartiles for numerical columns.
Step 10: Creates loss_df, a subset of df where profits are negative (indicating losses).
Step 11 Observation:
After grouping the loss data by Segment, we can observe the total losses attributed to each customer segment.
python
Total loss by Segment:
Segment
Consumer -11970.0560
Corporate -6943.0962
Home Office -4793.3329
Name: Profit, dtype: float64
Observation: The Consumer segment has the highest total losses, suggesting that individual consumers are contributing more to the losses than corporate clients or home offices.
Step 12 Observation:
Grouping by Sub-Category reveals the products that are most unprofitable.
python
Total loss by Sub-Category:
Sub-Category
Tables -17725.4811
Bookcases -3472.5560
Machines -2207.6292
Supplies -1965.3530
Fasteners -974.5730
Furnishings -928.5774
Accessories -517.5550
Labels -465.8622
Chairs -452.0382
Art -417.4780
Storage -242.8650
Envelopes -236.1540
Paper -104.5310
Name: Profit, dtype: float64
Note: The observations are based on the data provided and are meant to guide further analysis. It's important to investigate the underlying reasons for these trends to make informed business decisions.
Observation:Tables and Bookcases are the sub-categories with the highest losses, indicating that these products may need price adjustments, cost reduction, or marketing strategies to improve profitability.
Step 13 Observation:
Ranking sub-categories by the number of loss-making records helps identify which products frequently incur losses.
python
Sub-Categories ranked by number of loss-making records:
Product Name 66
Binders 63
Paper 61
Furnishings 60
Storage 57
Accessories 55
Phones 50
Art 50
Chairs 47
Appliances 47
Tables 44
Machines 41
Labels 39
Bookcases 38
Envelopes 24
Supplies 14
Fasteners 13
Copiers 2
Name: Sub-Category, dtype: int64
Observation:Binders and Paper have the highest number of loss-making transactions, which may suggest issues with pricing, discounts, or cost in these sub-categories.
Step 14 Observation:
Analyzing losses by city shows where the business is underperforming geographically.
python
Cities with the highest total losses:
City
Philadelphia -2441.2527
Houston -2026.1852
Chicago -1938.1485
Los Angeles -1903.0516
San Francisco -1775.8385
New York City -1236.0012
Jacksonville -915.6990
Seattle -907.3743
San Antonio -608.0310
Henderson -521.4048
Name: Profit, dtype: float64
Observation: Major cities like Philadelphia, Houston, and Chicago have the highest losses, indicating potential issues in these markets, such as high operational costs or competitive pressures.
Step 15 Observation:
Summarizing key metrics by region provides insight into regional performance.
python
Sales Quantity Discount Profit
Region
Central 501239.8901 9310 1355.66 39706.3625
East 678781.2000 11982 1556.87 9152.4697
South 391721.9050 6785 1106.74 46749.4303
West 725457.8245 11473 1567.64 108418.4489
Observation: The West region has the highest sales and profit, while the East region, despite high sales, shows significantly lower profits, suggesting higher costs or discounts affecting profitability.