Descriptive statistics help summarize and understand key aspects of a dataset. It includes measures such as mean, median, mode, standard deviation, and percentiles. Using NumPy, Pandas, and Seaborn, we can quickly analyze datasets and extract useful insights.
In this tutorial, we will cover:
Descriptive statistics summarize data without making predictions. Some commonly used measures include:
We will use the Pandas library for data summarization and Seaborn for visualization.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample dataset
df = sns.load_dataset("tips")
df.head()The mean is the sum of all values divided by the total count.
mean_value = df['total_bill'].mean()
print("Mean of total bill:", mean_value)The median is the middle value when data is sorted.
median_value = df['total_bill'].median()
print("Median of total bill:", median_value)The mode is the most frequently occurring value in a dataset.
mode_value = df['total_bill'].mode()[0]
print("Mode of total bill:", mode_value)Variance shows how far data points are from the mean.
variance_value = df['total_bill'].var()
print("Variance of total bill:", variance_value)Standard deviation measures the spread of data around the mean.
std_dev = df['total_bill'].std()
print("Standard Deviation of total bill:", std_dev)Range is the difference between the maximum and minimum values.
data_range = df['total_bill'].max() - df['total_bill'].min()
print("Range of total bill:", data_range)IQR measures the spread of the middle 50% of data (Q3 - Q1).
Q1 = df['total_bill'].quantile(0.25)
Q3 = df['total_bill'].quantile(0.75)
IQR = Q3 - Q1
print("Interquartile Range (IQR):", IQR)The .describe() function provides a quick summary of numerical columns.
df.describe()To get a summary of categorical columns, use:
df.describe(include=['O']) # 'O' stands for object (categorical)sns.histplot(df['total_bill'], bins=20, kde=True)
plt.title("Distribution of Total Bill")
plt.show()sns.boxplot(y=df['total_bill'])
plt.title("Box Plot of Total Bill")
plt.show()sns.pairplot(df)
plt.show()Percentiles help understand how data is distributed.
percentiles = df['total_bill'].quantile([0.25, 0.5, 0.75, 0.95])
print(percentiles).describe() function provides quick insights.Sign in to join the discussion and post comments.
Sign in