A Comprehensive Guide to Exploratory Data Analysis with an Example
Exploratory data analysis, or EDA for short, is a crucial step in the data science process that helps you gain a deeper understanding of your dataset before diving into machine learning or statistical modeling. By taking the time to explore and analyze your data upfront, you can uncover valuable insights, identify potential issues or outliers, and inform your strategy for the rest of the data science workflow.
In this comprehensive guide, we‘ll walk through the key concepts and techniques of exploratory data analysis, illustrated by a real-world example. Whether you‘re a data science beginner or an experienced practitioner looking to brush up on your EDA skills, this article will provide a helpful overview of this important topic.
The Goals of Exploratory Data Analysis
At its core, the purpose of exploratory data analysis is to get a sense of what your data looks like and what kinds of interesting insights might be hidden within it. Some of the key things you want to accomplish through EDA include:
- Getting an initial feel for the structure and content of your dataset, including data types, value ranges, distributions, etc.
- Checking data quality and cleaning the data as needed, such as handling missing values, fixing inconsistencies, removing duplicates, etc.
- Identifying noteworthy patterns, trends, relationships or anomalies in the data that warrant further investigation
- Determining which attributes or features seem to be most relevant or informative for the question at hand
- Forming hypotheses and getting ideas for what to analyze more deeply or build models to predict
EDA is all about being curious and inquisitive about your data. You should approach it with an open mind, following the data where it leads you and noting anything that sparks your interest or intuition as a data scientist.
Key Techniques for Exploratory Data Analysis
To achieve the goals outlined above, EDA employs a variety of useful techniques that generally fall into a few main categories:
Univariate Analysis
Univariate analysis explores individual attributes or variables in the dataset one at a time. This includes examining their data types, ranges of values, central tendencies, and distributions or frequencies.
Common univariate analysis techniques include:
- Descriptive statistics like mean, median, mode, min/max values, etc.
- Frequency distributions and histograms
- Box plots and violin plots to visualize distributions and outliers
Bivariate & Multivariate Analysis
Looking at attributes individually can only tell you so much. To get a more complete understanding, you need to analyze them in combination.
Bivariate analysis focuses on exploring the relationship between two attributes at a time, while multivariate analysis examines three or more attributes simultaneously. Prevalent techniques here include:
- Scatter plots and line plots
- Grouped bar charts and histograms
- Correlation matrices and heat maps
- Contour plots and 3D plots
Data Visualization
Data visualization goes hand-in-hand with exploratory analysis. Visuals help reveal patterns and relationships that may be hard to grasp from raw numbers alone. Plus, visualizations make it easy to share and communicate insights with others.
The best visualizations for EDA tend to be simple, intuitive, and clearly highlight the point you‘re trying to convey. Some essential chart types include:
- Line charts for time series and trends over time
- Bar charts for comparing discrete categories
- Scatter plots for relationships between two continuous variables
- Heat maps for relationships between several variables at once
Slicing & Dicing
Often the most illuminating insights come from segmenting your data in different ways and comparing subgroups. Slicing and dicing refers to breaking your data down by various dimensions or categories to examine patterns at a more granular level.
For example, an e-commerce company might slice their sales data by product category, customer demographics, marketing channels, etc. to see which are generating the most revenue. Or a healthcare provider could segment patient data by age groups, genders, conditions, etc. to compare treatments and outcomes.
A Real-World Example: Exploring Customer Purchasing Behavior
Let‘s make things concrete with a real dataset. For this example, we‘ll use a modified version of a Kaggle dataset on customer purchasing behavior. The dataset contains one year of transactional data for a UK-based online retailer, with information like:
- Invoice number and date
- Customer ID
- Product details (description, quantity, unit price)
- Country shipped to
After acquiring and loading the raw data, the first thing we‘ll do is get an overview of what‘s in the dataset. Examining the first few rows shows there are a total of 8 columns:
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country
536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6 12/1/2010 8:26 2.55 17850.0 United Kingdom
536365 71053 WHITE METAL LANTERN 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
536365 84406B CREAM CUPID HEARTS COAT HANGER 8 12/1/2010 8:26 2.75 17850.0 United Kingdom
536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
A DataFrame summary reveals a total of 541,909 records, with data types of object (string), int, float, and datetime. We also see that only the CustomerID column contains missing values, which is fine for this analysis.
Column Non-Null Count Data Type
InvoiceNo 541909 non-null object
StockCode 541909 non-null object
Description 540455 non-null object
Quantity 541909 non-null int64
InvoiceDate 541909 non-null datetime
UnitPrice 541909 non-null float64
CustomerID 406829 non-null float64
Country 541909 non-null object
Univariate Analysis
Diving into univariate analysis, let‘s examine the distribution of Quantity. A histogram shows the vast majority of order quantities are in the single digits, with a few orders up into the hundreds:

Calculating summary statistics confirms these observations:
count 541909.000
mean 9.552
std 218.081
min -80995.000
25% 1.000
50% 3.000
75% 10.000
max 80995.000
The extremely large max value of 80995 suggests a potential outlier or data entry error. Filtering the dataset to exclude the top and bottom 1% of Quantity values and re-running the histogram cleans up the distribution considerably.
Next let‘s look at the most frequent items sold using a bar plot of the top 10 Description values:

It seems the store sells a lot of gift bags, mugs, and t-light holders. But to get better insight we should look at total revenue, not just total number sold.
Bivariate Analysis
Moving into bivariate analysis, a scatter plot comparing Quantity vs UnitPrice reveals an interesting pattern:

There‘s a dense cluster of items with low prices and quantities in the single digits to tens range, and then some very expensive items in extremely large quantities as outliers in the upper right. This suggests some items are bought individually by regular customers while others are likely purchased in bulk for resale, promotions, etc.
To dig deeper, let‘s combine Quantity and UnitPrice to calculate total revenue per transaction, then group by CustomerID to get each customer‘s all-time total spend. Plotting this as a histogram reveals a classic "long tail" distribution:

The vast majority of customers have spent less than £1000 in their lifetimes so far, while a handful of extremely high rollers have spent tens of thousands of pounds. This suggests it could be valuable to develop separate marketing strategies for small vs large spenders.
Multivariate Analysis
Finally, let‘s bring in a third variable and look at purchasing patterns by Country. A bar chart of average order value by country reveals that while most sales come from the UK, a handful of other countries have significantly higher typical order sizes:

Japan, Ireland, and Singapore stand out as having 2-3x the typical order size vs the UK. Breaking this down further into a heatmap of total sales by Country and product Description shows these high-value orders are concentrated in specific items:

Paper craft little birdie stickers are popular in Japan, retro chocolate tin canisters sell well in Ireland, and Singapore favors art glass domes. This level of insight could help inform country-specific product recommendations or promotions.
Conclusions and Next Steps
Our exploratory analysis uncovered several intriguing insights about this online retailer‘s sales patterns that we could dig into further or use to inform business decisions:
- Most customers purchase small quantities of inexpensive gift-shop type items, but a few big spenders drive large bulk orders
- A "long tail" of customers accounts for the majority of lifetime revenue, suggesting it may be prudent to develop separate strategies for frequent vs occasional buyers
- Certain products sell especially well in specific countries outside the core UK market, pointing to opportunities for targeted marketing
Of course, we‘ve only just scratched the surface in this quick example. Further exploration could lead to ideas for predictive modeling, like classifying customers into segments based on purchase history and demographics. We could also conduct more granular analysis of sales trends over time, looking at daily/weekly/seasonal patterns.
The key takeaway is that EDA is a powerful tool for generating quick insights and spurring ideas to test more rigorously later in the data science process. By taking the time to slice, dice, and visualize your data upfront, you equip yourself to ask better questions and ultimately build more effective models down the line.
I hope this guide has given you a solid foundation in exploratory data analysis to apply to your own datasets. The techniques and thought process we walked through are infinitely customizable to your specific needs and can help add a whole new dimension to your data science projects. Now get out there and explore your data – happy analyzing!