📖 Scenario: You work as a data analyst for a retail company. You have sales data for different stores. Sometimes, some sales numbers are unusually high or low. These are called outliers. Detecting outliers helps you understand if there are errors or special cases in the data.
🎯 Goal: You will learn how to detect outliers in sales data using the Interquartile Range (IQR) method with pandas. You will create the sales data, calculate IQR, find outliers, and display them.
📋 What You'll Learn
Use pandas to create and analyze data
Calculate quartiles and IQR
Detect outliers using IQR method
Print the detected outliers
💡 Why This Matters
🌍 Real World
Outlier detection helps businesses find unusual data points that may indicate errors, fraud, or special events.
💼 Career
Data analysts and data scientists often use IQR to clean data and improve the quality of their analysis.
Progress0 / 4 steps
1
Create sales data with pandas
Create a pandas DataFrame called sales_data with one column named sales and these exact values: 100, 150, 200, 250, 300, 350, 400, 1000.
Pandas
Hint
Use pd.DataFrame with a dictionary where the key is "sales" and the value is the list of numbers.
2
Calculate Q1, Q3 and IQR
Create three variables: Q1, Q3, and IQR. Calculate Q1 as the 25th percentile of sales_data["sales"], Q3 as the 75th percentile, and IQR as Q3 - Q1.
Pandas
Hint
Use the quantile() method on the sales column to get Q1 and Q3.
3
Detect outliers using IQR
Create a variable called outliers that contains the rows from sales_data where the sales value is less than Q1 - 1.5 * IQR or greater than Q3 + 1.5 * IQR.
Pandas
Hint
Use boolean conditions combined with | inside the DataFrame filter brackets.
4
Print the detected outliers
Write a print statement to display the outliers DataFrame.
Pandas
Hint
Use print(outliers) to show the rows with outliers.
Practice
(1/5)
1. What does the Interquartile Range (IQR) represent in a dataset?
easy
A. The range between the 25th percentile (Q1) and the 75th percentile (Q3)
B. The difference between the maximum and minimum values
C. The average value of the dataset
D. The total number of data points
Solution
Step 1: Understand quartiles
Quartiles divide data into four equal parts. Q1 is the 25th percentile, Q3 is the 75th percentile.
Step 2: Define IQR
IQR is the difference between Q3 and Q1, showing the middle 50% spread of data.
Final Answer:
The range between the 25th percentile (Q1) and the 75th percentile (Q3) -> Option A
Quick Check:
IQR = Q3 - Q1 [OK]
Hint: IQR = Q3 minus Q1, middle half range [OK]
Common Mistakes:
Confusing IQR with total range
Thinking IQR is the average
Mixing up quartiles with percentiles
2. Which of the following pandas code snippets correctly calculates the IQR of a DataFrame column df['score']?
easy
A. iqr = df['score'].mean() - df['score'].median()
B. iqr = df['score'].max() - df['score'].min()
C. iqr = df['score'].quantile(0.75) - df['score'].quantile(0.25)
D. iqr = df['score'].std()
Solution
Step 1: Recall IQR formula
IQR is Q3 minus Q1, where Q3 is 75th percentile and Q1 is 25th percentile.
Step 2: Check pandas quantile usage
Using quantile(0.75) and quantile(0.25) correctly gets Q3 and Q1.
Final Answer:
iqr = df['score'].quantile(0.75) - df['score'].quantile(0.25) -> Option C
Quick Check:
Use quantile(0.75) - quantile(0.25) for IQR [OK]
Hint: Use quantile(0.75) minus quantile(0.25) for IQR [OK]
Common Mistakes:
Using mean or median instead of quantiles
Using max-min instead of IQR
Using standard deviation instead of IQR
3. Given the following code, what will be the output of outliers?
D. The condition uses '&' instead of '|' to combine bounds
Solution
Step 1: Understand outlier condition
Outliers are values less than lower bound OR greater than upper bound, so conditions should be combined with OR (|).
Step 2: Check code condition
The code uses AND (&), which means no value can be both greater than upper bound and less than lower bound simultaneously.
Final Answer:
The condition uses '&' instead of '|' to combine bounds -> Option D
Quick Check:
Use '|' for outlier condition, not '&' [OK]
Hint: Use OR (|) to combine outlier conditions, not AND (&) [OK]
Common Mistakes:
Using AND instead of OR for outlier filtering
Swapping Q1 and Q3 values
Calculating IQR incorrectly
5. You have a DataFrame df with a column 'height'. You want to remove outliers using IQR but keep the original index. Which code correctly filters out outliers and keeps the index intact?