Analyzing Diamond Prices with Box Plots

Topic Overview

Hello and welcome! In today's lesson, we will focus on using box plots to analyze the prices of diamonds based on their cut quality. Box plots are an effective way to visualize the distribution of a dataset and can help you extract meaningful insights. Our main goal is to create a box plot that illustrates how diamond prices vary according to cut quality and to learn how to interpret this visualization.

Preparing the Data

Before plotting our data, it's important to ensure it's clean. Although you already understand data cleaning, let's briefly revisit it in context.

To ensure our dataset is clean:

  1. We need to filter out any entries with missing values.
  2. We will then confirm that the dataset is clean by inspecting the first few rows and the total number of entries after cleaning.
import seaborn as sns
import matplotlib.pyplot as plt

# Load the diamonds dataset
diamonds = sns.load_dataset('diamonds')

# Filter out any entries with missing values
diamonds = diamonds.dropna()

# Check the dataset after cleaning
print(diamonds.head())
print(f"Total number of cleaned entries: {diamonds.shape[0]}")
print(diamonds.isnull().sum())

The output of the above code will be:

   carat      cut color clarity  depth  table  price     x     y     z
0   0.23    Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1   0.21  Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2   0.23     Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3   0.29  Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4   0.31     Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
Total number of cleaned entries: 53940
carat      0
cut        0
color      0
clarity    0
depth      0
table      0
price      0
x          0
y          0
z          0
dtype: int64

This output confirms that our dataset is now clean, free from missing values, and ready for further analysis.

Creating the Box Plot

Now, we will create a box plot to visualize the distribution of diamond prices across different cut categories.

  1. Objective: To compare the distribution of diamond prices based on their cut quality.
  2. Setup: We will use Seaborn's boxplot function to generate the plot.
  3. Customization: Set the figure size for better readability and add titles and labels for clarity.
import seaborn as sns
import matplotlib.pyplot as plt

# Load the diamonds dataset
diamonds = sns.load_dataset('diamonds')

# Filter out any entries with missing values
diamonds = diamonds.dropna()

# Box plot of prices by cut
plt.figure(figsize=(10,6))
sns.boxplot(x='cut', y='price', data=diamonds)
plt.title('Box Plot of Prices by Cut')
plt.xlabel('Cut')
plt.ylabel('Price')
plt.show()

The output will be an informative visual representation showing the distribution of diamond prices by cut quality, with box plots for each category.

This creates a box plot where:

  • The x-axis represents the different cut categories (Fair, Good, Very Good, Premium, Ideal).
  • The y-axis represents the price of diamonds.
  • Each box plot shows the distribution of prices for a specific cut category by representing the spread and central tendency.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal