Md Mominul Islam | Software and Data Enginnering | SQL Server, .NET, Power BI, Azure Blog

while(!(succeed=try()));

LinkedIn Portfolio Banner

Latest

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Friday, August 22, 2025

Master Data Analysis: Module 6 - Comprehensive Guide to Exploratory Data Analysis (EDA) with Python & SQL

 


Table of Contents

  1. Introduction to Exploratory Data Analysis (EDA)

  2. Summary Statistics: Understanding Your Data

    • 2.1 Mean, Median, Mode

    • 2.2 Variance and Standard Deviation

    • 2.3 Real-Life Example: Retail Sales Analysis

    • 2.4 Python Code for Summary Statistics

    • 2.5 SQL Code for Summary Statistics

  3. Correlation Analysis and Covariance

    • 3.1 Understanding Correlation and Covariance

    • 3.2 Real-Life Example: E-commerce Customer Behavior

    • 3.3 Python Code for Correlation Analysis

    • 3.4 SQL Code for Correlation Analysis

  4. Grouping, Aggregation, and Pivot Tables

    • 4.1 Grouping and Aggregation Concepts

    • 4.2 Pivot Tables for Multidimensional Analysis

    • 4.3 Real-Life Example: Healthcare Patient Data

    • 4.4 Python Code for Grouping and Pivot Tables

    • 4.5 SQL Code for Grouping and Pivot Tables

  5. Identifying Trends, Patterns, and Anomalies

    • 5.1 Techniques for Trend and Pattern Detection

    • 5.2 Anomaly Detection Methods

    • 5.3 Real-Life Example: Financial Fraud Detection

    • 5.4 Python Code for Trend and Anomaly Detection

  6. Using Python Libraries for EDA: Pandas and NumPy

    • 6.1 Why Pandas and NumPy?

    • 6.2 Best Practices for Using Pandas and NumPy

    • 6.3 Exception Handling in Python

  7. Pros, Cons, and Alternatives

    • 7.1 Advantages of EDA with Python and SQL

    • 7.2 Limitations and Challenges

    • 7.3 Alternatives to Pandas, NumPy, and SQL

  8. Conclusion and Next Steps


1. Introduction to Exploratory Data Analysis (EDA)

Exploratory Data Analysis (EDA) is the process of analyzing datasets to summarize their main characteristics, often using visual and statistical methods. It’s the first step in understanding your data before applying advanced modeling techniques. EDA helps uncover patterns, detect anomalies, test hypotheses, and check assumptions, making it a cornerstone of data analysis.

In this module, we’ll explore EDA through summary statistics, correlation analysis, grouping, pivot tables, and trend/anomaly detection. Using Python’s Pandas and NumPy libraries and SQL, we’ll provide practical, real-world examples to make the concepts accessible and engaging. Whether you’re analyzing retail sales, healthcare data, or financial transactions, this guide will equip you with the tools and best practices to extract meaningful insights.


2. Summary Statistics: Understanding Your Data

Summary statistics provide a quick snapshot of your data’s central tendencies and variability. They include measures like mean, median, mode, variance, and standard deviation, which help you understand the distribution and spread of your data.

2.1 Mean, Median, Mode

  • Mean: The average value, calculated by summing all values and dividing by the count.

  • Median: The middle value when data is sorted, robust to outliers.

  • Mode: The most frequent value in the dataset.

2.2 Variance and Standard Deviation

  • Variance: Measures how much the data points deviate from the mean.

  • Standard Deviation: The square root of variance, indicating data spread in the same units as the data.

2.3 Real-Life Example: Retail Sales Analysis

Imagine you’re a data analyst at a retail chain analyzing monthly sales data to understand store performance. You want to calculate summary statistics to identify top-performing stores and detect underperforming ones.

Dataset: A CSV file retail_sales.csv with columns: store_id, month, sales_amount, customer_count.

2.4 Python Code for Summary Statistics

import pandas as pd
import numpy as np

# Load the dataset
try:
    df = pd.read_csv('retail_sales.csv')
except FileNotFoundError:
    print("Error: retail_sales.csv not found. Please check the file path.")
    exit()

# Calculate summary statistics
mean_sales = df['sales_amount'].mean()
median_sales = df['sales_amount'].median()
mode_sales = df['sales_amount'].mode()[0]
variance_sales = df['sales_amount'].var()
std_sales = df['sales_amount'].std()

# Display results
print(f"Mean Sales: ${mean_sales:.2f}")
print(f"Median Sales: ${median_sales:.2f}")
print(f"Mode Sales: ${mode_sales:.2f}")
print(f"Variance of Sales: {variance_sales:.2f}")
print(f"Standard Deviation of Sales: ${std_sales:.2f}")

# Group by store and calculate summary statistics
store_stats = df.groupby('store_id')['sales_amount'].agg(['mean', 'median', 'std']).round(2)
print("\nStore-wise Summary Statistics:")
print(store_stats)

Explanation:

  • We load the dataset using pd.read_csv() with error handling for missing files.

  • Pandas functions like mean(), median(), mode(), var(), and std() compute summary statistics.

  • Grouping by store_id provides store-specific insights, useful for identifying high or low performers.

2.5 SQL Code for Summary Statistics

SELECT 
    store_id,
    AVG(sales_amount) AS mean_sales,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY sales_amount) AS median_sales,
    MODE() WITHIN GROUP (ORDER BY sales_amount) AS mode_sales,
    VARIANCE(sales_amount) AS variance_sales,
    STDDEV(sales_amount) AS std_sales
FROM retail_sales
GROUP BY store_id;

Explanation:

  • SQL’s AVG, PERCENTILE_CONT, MODE, VARIANCE, and STDDEV functions compute summary statistics.

  • Grouping by store_id mirrors the Python example, providing store-level insights.

  • Use TRY-CATCH blocks in production SQL environments to handle errors like missing tables.

Best Practices:

  • Always validate data types before calculations (e.g., ensure sales_amount is numeric).

  • Handle missing values using df.dropna() in Python or COALESCE in SQL.

  • Round results to two decimal places for readability.


3. Correlation Analysis and Covariance

Correlation and covariance measure relationships between variables, helping you understand how they move together.

3.1 Understanding Correlation and Covariance

  • Correlation: Measures the strength and direction of a linear relationship between two variables (range: -1 to 1).

  • Covariance: Indicates whether two variables increase or decrease together, but without a standardized scale.

3.2 Real-Life Example: E-commerce Customer Behavior

You’re analyzing an e-commerce dataset to see if time_spent_on_site correlates with purchase_amount. A strong positive correlation could inform marketing strategies.

Dataset: ecommerce_data.csv with columns: user_id, time_spent_on_site, purchase_amount.

3.3 Python Code for Correlation Analysis

import pandas as pd
import numpy as np

# Load the dataset
try:
    df = pd.read_csv('ecommerce_data.csv')
except FileNotFoundError:
    print("Error: ecommerce_data.csv not found.")
    exit()

# Calculate correlation
correlation = df['time_spent_on_site'].corr(df['purchase_amount'])
covariance = np.cov(df['time_spent_on_site'], df['purchase_amount'])[0][1]

print(f"Correlation between Time Spent and Purchase Amount: {correlation:.2f}")
print(f"Covariance between Time Spent and Purchase Amount: {covariance:.2f}")

# Visualize correlation with a scatter plot
import matplotlib.pyplot as plt
plt.scatter(df['time_spent_on_site'], df['purchase_amount'])
plt.xlabel('Time Spent on Site (minutes)')
plt.ylabel('Purchase Amount ($)')
plt.title('Correlation Analysis: Time Spent vs. Purchase Amount')
plt.show()

Explanation:

  • corr() computes Pearson’s correlation coefficient.

  • np.cov() calculates covariance.

  • A scatter plot visualizes the relationship, making it easier to interpret.

3.4 SQL Code for Correlation Analysis

SELECT 
    (AVG(time_spent_on_site * purchase_amount) - 
     AVG(time_spent_on_site) * AVG(purchase_amount)) /
    (STDDEV(time_spent_on_site) * STDDEV(purchase_amount)) AS correlation
FROM ecommerce_data;

Explanation:

  • SQL computes correlation manually using the formula for Pearson’s correlation.

  • Use subqueries or temporary tables for complex calculations in production.

Best Practices:

  • Check for outliers, as they can skew correlation results (use df.describe() or SQL PERCENTILE_CONT).

  • Ensure variables are numeric and handle missing values.

  • Visualize correlations with heatmaps for multiple variables.


4. Grouping, Aggregation, and Pivot Tables

Grouping and aggregation summarize data by categories, while pivot tables provide multidimensional views.

4.1 Grouping and Aggregation Concepts

  • Grouping: Organizes data into groups based on one or more columns.

  • Aggregation: Applies functions (e.g., sum, mean) to grouped data.

4.2 Pivot Tables for Multidimensional Analysis

Pivot tables summarize data across multiple dimensions, making it easier to spot trends.

4.3 Real-Life Example: Healthcare Patient Data

You’re analyzing patient data to understand hospital readmission rates by age group and diagnosis.

Dataset: patient_data.csv with columns: patient_id, age_group, diagnosis, readmission_count.

4.4 Python Code for Grouping and Pivot Tables

import pandas as pd

# Load the dataset
try:
    df = pd.read_csv('patient_data.csv')
except FileNotFoundError:
    print("Error: patient_data.csv not found.")
    exit()

# Grouping and aggregation
group_stats = df.groupby(['age_group', 'diagnosis'])['readmission_count'].agg(['mean', 'sum']).round(2)
print("Grouped Statistics:")
print(group_stats)

# Pivot table
pivot_table = df.pivot_table(values='readmission_count', index='age_group', columns='diagnosis', aggfunc='mean').round(2)
print("\nPivot Table:")
print(pivot_table)

# Handle missing values in pivot table
pivot_table.fillna(0, inplace=True)

Explanation:

  • groupby() groups data by age_group and diagnosis, with aggregation functions like mean and sum.

  • pivot_table() creates a matrix view, with age_group as rows and diagnosis as columns.

  • fillna(0) handles missing values in the pivot table.

4.5 SQL Code for Grouping and Pivot Tables

-- Grouping and aggregation
SELECT 
    age_group,
    diagnosis,
    AVG(readmission_count) AS avg_readmissions,
    SUM(readmission_count) AS total_readmissions
FROM patient_data
GROUP BY age_group, diagnosis;

-- Pivot table (using CASE statements for SQL Server)
SELECT 
    age_group,
    AVG(CASE WHEN diagnosis = 'Diabetes' THEN readmission_count END) AS Diabetes,
    AVG(CASE WHEN diagnosis = 'Heart Disease' THEN readmission_count END) AS Heart_Disease
FROM patient_data
GROUP BY age_group;

Explanation:

  • SQL’s GROUP BY mirrors Pandas’ groupby().

  • Pivot tables in SQL use CASE statements or PIVOT operators (database-dependent).

  • Handle NULLs with COALESCE or ISNULL.

Best Practices:

  • Validate categorical columns for consistency (e.g., no duplicate spellings).

  • Use meaningful aggregation functions based on the use case.

  • Optimize SQL queries with indexes for large datasets.


5. Identifying Trends, Patterns, and Anomalies

Trends and patterns reveal how data evolves over time, while anomalies indicate unusual data points.

5.1 Techniques for Trend and Pattern Detection

  • Moving Averages: Smooth data to identify trends.

  • Visualization: Use line plots or heatmaps to spot patterns.

5.2 Anomaly Detection Methods

  • Z-Score: Identifies data points far from the mean.

  • IQR Method: Detects outliers using interquartile range.

5.3 Real-Life Example: Financial Fraud Detection

You’re analyzing credit card transactions to detect fraudulent activities based on unusual transaction amounts.

Dataset: transactions.csv with columns: transaction_id, date, amount.

5.4 Python Code for Trend and Anomaly Detection

import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset
try:
    df = pd.read_csv('transactions.csv')
except FileNotFoundError:
    print("Error: transactions.csv not found.")
    exit()

# Calculate moving average for trend
df['moving_avg'] = df['amount'].rolling(window=7).mean()

# Plot trends
plt.plot(df['date'], df['amount'], label='Transaction Amount')
plt.plot(df['date'], df['moving_avg'], label='7-Day Moving Average', color='red')
plt.xlabel('Date')
plt.ylabel('Transaction Amount ($)')
plt.title('Transaction Trends')
plt.legend()
plt.show()

# Anomaly detection using Z-score
df['z_score'] = (df['amount'] - df['amount'].mean()) / df['amount'].std()
anomalies = df[abs(df['z_score']) > 3]

print("Detected Anomalies:")
print(anomalies[['transaction_id', 'date', 'amount', 'z_score']])

Explanation:

  • rolling(window=7) computes a 7-day moving average to smooth trends.

  • Z-score identifies transactions more than 3 standard deviations from the mean.

  • Visualizations make trends and anomalies intuitive.

Best Practices:

  • Choose appropriate window sizes for moving averages based on data frequency.

  • Validate anomaly thresholds (e.g., Z-score > 3) with domain experts.

  • Use robust methods like IQR for non-normal data.


6. Using Python Libraries for EDA: Pandas and NumPy

Pandas and NumPy are powerful Python libraries for data manipulation and numerical computations.

6.1 Why Pandas and NumPy?

  • Pandas: Provides DataFrames for tabular data, with intuitive functions for filtering, grouping, and pivoting.

  • NumPy: Offers efficient array operations for numerical calculations like correlation and variance.

6.2 Best Practices for Using Pandas and NumPy

  • Use vectorized operations instead of loops for performance.

  • Handle missing data with dropna() or fillna().

  • Optimize memory usage with appropriate data types (e.g., float32 instead of float64).

6.3 Exception Handling in Python

import pandas as pd
import numpy as np

# Example with exception handling
try:
    df = pd.read_csv('data.csv')
    # Convert to appropriate data type
    df['amount'] = df['amount'].astype(np.float32)
    # Handle missing values
    df['amount'].fillna(df['amount'].mean(), inplace=True)
    # Perform calculation
    mean_amount = df['amount'].mean()
    print(f"Mean Amount: ${mean_amount:.2f}")
except FileNotFoundError:
    print("Error: Data file not found.")
except ValueError as e:
    print(f"Error: Invalid data format - {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Explanation:

  • Catch specific exceptions like FileNotFoundError and ValueError for robust code.

  • Use astype() to optimize data types.

  • Handle missing values to prevent calculation errors.


7. Pros, Cons, and Alternatives

7.1 Advantages of EDA with Python and SQL

  • Python: Flexible, rich visualization libraries, community support.

  • SQL: Scalable for large datasets, integrates with databases.

7.2 Limitations and Challenges

  • Python: Memory-intensive for large datasets, slower for big data.

  • SQL: Limited visualization, complex for advanced computations.

7.3 Alternatives to Pandas, NumPy, and SQL

  • Polars: Faster than Pandas for large datasets.

  • Dask: Scales Pandas to big data.

  • Spark SQL: Handles massive datasets with distributed computing.


8. Conclusion and Next Steps

Exploratory Data Analysis is a critical skill for uncovering insights from data. By mastering summary statistics, correlation analysis, grouping, pivot tables, and anomaly detection with Python and SQL, you’re equipped to tackle real-world problems in retail, healthcare, finance, and more. Practice with the provided code examples, adopt best practices, and explore alternatives like Polars or Spark for advanced use cases.

No comments:

Post a Comment

Thanks for your valuable comment...........
Md. Mominul Islam

Post Bottom Ad

Responsive Ads Here