0% found this document useful (0 votes)
2 views27 pages

Data Analyst Course Vol3 Python

The document outlines Volume 3 of 'The Complete Data Analyst Course', focusing on Python for data analysis, covering fundamentals, NumPy, Pandas, data cleaning, and visualization. It is structured for self-learners aiming for data analyst roles, providing clear explanations, code examples, and practice exercises. Each chapter builds on prior knowledge from earlier volumes, emphasizing hands-on coding in Jupyter Notebook.

Uploaded by

bclassdrtamultan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

Data Analyst Course Vol3 Python

The document outlines Volume 3 of 'The Complete Data Analyst Course', focusing on Python for data analysis, covering fundamentals, NumPy, Pandas, data cleaning, and visualization. It is structured for self-learners aiming for data analyst roles, providing clear explanations, code examples, and practice exercises. Each chapter builds on prior knowledge from earlier volumes, emphasizing hands-on coding in Jupyter Notebook.

Uploaded by

bclassdrtamultan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

THE COMPLETE

DATA ANALYST
COURSE
VOLUME 3
Python for Data Analysis

CH 1 Python Fundamentals -- Variables, Loops, Functions


CH 2 NumPy -- Fast Numerical Computing
CH 3 Pandas -- DataFrames, Filtering, GroupBy, Merge
CH 4 Data Cleaning -- Missing Values, Duplicates, Outliers
CH 5 Visualization -- Matplotlib & Seaborn

A Beginner-to-Advanced, Job-Ready Series


Written for self-learners targeting remote & international Data Analyst roles
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Table of Contents

PYTHON FOR DATA ANALYSIS


Chapter 1: Python Fundamentals
Chapter 2: NumPy — Fast Numerical Computing
Chapter 3: Pandas — The Data Analyst's Most Important Tool
Chapter 4: Data Cleaning with Pandas
Chapter 5: Data Visualization with Matplotlib & Seaborn

How This Volume Is Organized


This is Volume 3 of the Complete Data Analyst Course series, following Volume 1 (Foundations, Tools &
Excel Mastery) and Volume 2 (SQL for Data Analysis). This volume covers Python from absolute basics
through the complete data analysis toolkit: NumPy, Pandas, data cleaning, and visualization with
Matplotlib and Seaborn.

Every chapter follows the same structure used throughout this series: clear explanation → worked code
example → common mistakes to avoid → practice exercises and mini assignments. Many sections
explicitly connect new Python concepts back to the Excel formulas (Volume 1) and SQL queries (Volume
2) you've already learned, since the underlying thinking is often identical — only the syntax changes.

Recommended approach: open a Jupyter Notebook (set up in Volume 1, Chapter 6) and type every
example yourself as you read. Python, like SQL, is learned by running code and seeing real output — not
by reading alone.

2
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PART 1
Python Fundamentals
Variables, data types, loops, functions, and importing libraries

Chapter 1: Python Fundamentals


Python is the most widely used programming language in data analysis because its syntax reads almost
like plain English, and it has powerful free libraries (NumPy, Pandas, Matplotlib, Seaborn) built
specifically for working with data. This chapter assumes zero prior programming experience and builds
everything from the ground up.

Recall from Volume 1 that Python and Jupyter Notebook are already installed and ready to use. Open a
new Jupyter Notebook now and type every example in this chapter yourself as you read — programming
is learned by typing code and seeing what happens, not by reading alone.

Variables: Storing Data


A variable is a named container that stores a value. In Python, you create a variable simply by writing a
name, an equals sign, and a value — no special declaration needed.

PYTHON

name = "Ali"
age = 28
salary = 75000.50
is_employed = True

print(name)
print(age)

Variable names should be descriptive (use customer_name instead of x), start with a letter or
underscore, and cannot contain spaces — use underscores instead (total_revenue, not total
revenue).

3
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Core Data Types


Type Example Used For

int 28, -5, 1000 Whole numbers

float 75000.50, 3.14 Decimal numbers

str "Ali", 'Multan' Text

bool True, False Yes/no, conditions

list [1, 2, 3] Ordered collections of items

dict {"name": "Ali"} Key-value pairs

Checking a Variable's Type

PYTHON

print(type(28)) # <class 'int'>


print(type(75000.50)) # <class 'float'>
print(type("Ali")) # <class 'str'>
print(type(True)) # <class 'bool'>

Lists: Ordered Collections


A list stores multiple values in a single variable, in order, and is one of the most-used data structures in
Python. Lists are written with square brackets, items separated by commas.

PYTHON

sales = [1200, 1500, 980, 2200, 1750]

print(sales[0]) # First item -> 1200 (Python counts from 0, not 1)


print(sales[-1]) # Last item -> 1750
print(len(sales)) # Number of items -> 5

[Link](1900) # Add a new item to the end


print(sales) # [1200, 1500, 980, 2200, 1750, 1900]

Common Mistake
Python counts positions starting from 0, not 1. The first item in a list is list[0], not list[1]. This trips up
almost every beginner at least once — if you get an 'index out of range' error, check whether you're
off by one.

4
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Dictionaries: Key-Value Pairs


A dictionary stores data as labeled pairs — each value has a named key, similar to a single row in a
spreadsheet where column names are the keys.

PYTHON

customer = {
"name": "Sara Khan",
"city": "Lahore",
"total_orders": 5
}

print(customer["name"]) # Sara Khan


print(customer["total_orders"]) # 5

customer["total_orders"] = 6 # Update a value


customer["country"] = "Pakistan" # Add a new key

Operators: Doing Math and Comparisons


Operator Meaning Example

+ - * / Add, subtract, multiply, divide 10 + 5 → 15

// Floor division (drops the remainder) 17 // 5 → 3

% Modulo (remainder after division) 17 % 5 → 2

** Exponent (power) 2 ** 3 → 8

== != Equal to / not equal to 5 == 5 → True

> < >= <= Greater/less than (or equal) 10 > 7 → True

Conditional Logic: if, elif, else


Conditional statements let your code make decisions — the direct equivalent of Excel's IF formula
(Volume 1) and SQL's CASE WHEN (Volume 2).

5
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

revenue = 850

if revenue > 1000:


print("High performer")
elif revenue > 500:
print("Average performer")
else:
print("Needs improvement")

# Output: Average performer

Tip for Beginners


Python uses indentation (spaces at the start of a line) instead of curly braces {} to mark what
belongs inside an if-block. Always indent consistently — Jupyter and VS Code do this automatically
when you press Enter after a colon, but mixing tabs and spaces manually is a very common source
of errors.

Loops: Repeating Actions


Loops let you run the same block of code multiple times — once for every item in a list, or a fixed number
of times. This is how you process data row by row without writing repetitive code.

for Loops

PYTHON

sales = [1200, 1500, 980, 2200, 1750]

for amount in sales:


print(amount)

# Calculate a running total


total = 0
for amount in sales:
total = total + amount
print("Total sales:", total) # Total sales: 7630

6
The Complete Data Analyst Course Volume 3: Python for Data Analysis

while Loops

PYTHON

count = 0
while count < 5:
print("Count is:", count)
count = count + 1
# Runs until the condition becomes False

Functions: Reusable Blocks of Code


A function packages a block of code under a name, so you can run it again anywhere without retyping it.
Functions can accept inputs (called parameters) and return an output.

PYTHON

def calculate_discount(price, discount_percent):


discount_amount = price * (discount_percent / 100)
final_price = price - discount_amount
return final_price

result = calculate_discount(1000, 15)


print(result) # 850.0

TIP: Why This Matters


Every Pandas and NumPy operation you'll use in the rest of this volume is, underneath, a function
call — something like [Link]() or [Link]([1,2,3]). Understanding what a function actually does
(accepts input, runs code, returns output) makes every future chapter far less mysterious.

Importing Libraries
A library is a collection of pre-written functions that someone else built, which you can use for free.
NumPy, Pandas, Matplotlib, and Seaborn (covered in this volume) are all libraries. You import them once
at the top of your notebook.

7
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns

# 'as np' lets you type np instead of numpy every time -- this exact
# import style (np, pd, plt, sns) is the universal standard, used in
# virtually every data analysis notebook in the world.

Mini Assignment
Open a new Jupyter Notebook. Create a list of 7 numbers representing one week's daily sales. Write
a for loop that prints each day's sales, then write a second loop that calculates and prints the total
and average sales for the week. Do this using only plain Python (no NumPy yet) to make sure these
fundamentals feel solid before Chapter 2.

Practice Exercise – Chapter 1


1. Create a dictionary representing one product with keys: name, price, quantity_in_stock.
2. Write an if/elif/else block that prints 'Restock needed' if quantity_in_stock is below 10, 'Low stock'
if below 50, otherwise 'Stock OK'.
3. Write a function called total_value(price, quantity) that returns price multiplied by quantity.
4. Create a list of 5 product prices and write a loop that prints only the prices above 100.

8
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PART 2
NumPy
Fast numerical computing with arrays -- the foundation Pandas is built on

Chapter 2: NumPy — Fast Numerical Computing


NumPy (Numerical Python) is the foundation that almost every other Python data library is built on,
including Pandas. Its core feature is the array — a grid of numbers that performs math operations
dramatically faster than plain Python lists, especially on large datasets. As a Data Analyst, you won't use
raw NumPy as often as Pandas, but every Pandas operation runs on NumPy underneath, so
understanding it makes everything else click.

Creating Arrays
PYTHON

import numpy as np

sales = [Link]([1200, 1500, 980, 2200, 1750])


print(sales)
print(type(sales)) # <class '[Link]'>

Why Not Just Use a Python List?


A plain Python list cannot do math across all its items at once — you'd need a loop. A NumPy array can,
and the difference becomes dramatic on large datasets (millions of rows), where NumPy can be 10 to
100 times faster.

PYTHON

sales_list = [1200, 1500, 980, 2200, 1750]


sales_array = [Link](sales_list)

# Add a 10% bonus to every value


# With a plain list, you'd need a loop:
bonus_list = [x * 1.1 for x in sales_list]

# With a NumPy array, this works directly on the whole array:


bonus_array = sales_array * 1.1
print(bonus_array) # [1320. 1650. 1078. 2420. 1925.]

9
The Complete Data Analyst Course Volume 3: Python for Data Analysis

TIP: Why This Matters


This 'apply an operation to every item at once' behavior is called vectorization, and it's the single
biggest reason NumPy (and Pandas, built on top of it) is so much faster than plain Python loops for
data work. Whenever you find yourself writing a loop to do math on every item in a dataset, there is
almost always a faster vectorized NumPy/Pandas way to do it.

Array Math Operations


PYTHON

prices = [Link]([10, 20, 30, 40])


quantities = [Link]([2, 1, 5, 3])

revenue = prices * quantities # Element-by-element multiplication


print(revenue) # [20 20 150 120]

print(prices + 5) # Add 5 to every price -> [15 25 35 45]


print([Link]()) # Total of all prices -> 100
print([Link]()) # Average price -> 25.0
print([Link]()) # Highest price -> 40
print([Link]()) # Lowest price -> 10

Useful NumPy Statistical Functions


Function Purpose Example

[Link](arr) Average value [Link](prices)

[Link](arr) Middle value when sorted [Link](prices)

[Link](arr) Standard deviation (spread of data) [Link](prices)

[Link](arr) Total of all values [Link](prices)

[Link](arr) Returns a sorted copy [Link](prices)

[Link](arr, n) Rounds every value to n decimals [Link](prices, 2)

10
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Creating Arrays with Built-In Generators


PYTHON

zeros = [Link](5) # [0. 0. 0. 0. 0.]


ones = [Link](5) # [1. 1. 1. 1. 1.]
sequence = [Link](0, 10, 2) # [0 2 4 6 8] -- start, stop, step
evenly_spaced = [Link](0, 1, 5) # [0. 0.25 0.5 0.75 1.]

These are most commonly used to generate test data, build axis values for charts (covered in Chapter 5),
or set default starting values before filling in real data.

2D Arrays: Rows and Columns


NumPy arrays can have multiple dimensions. A 2D array behaves like a simple spreadsheet grid with
rows and columns — this is conceptually the foundation that a Pandas DataFrame (Chapter 3) builds on.

PYTHON

grid = [Link]([
[10, 20, 30],
[40, 50, 60]
])

print([Link]) # (2, 3) -- 2 rows, 3 columns


print(grid[0]) # First row -> [10 20 30]
print(grid[0, 1]) # Row 0, Column 1 -> 20
print([Link]()) # Sum of everything -> 210
print([Link](axis=0)) # Sum down each column -> [50 70 90]
print([Link](axis=1)) # Sum across each row -> [60 150]

Common Mistake
axis=0 and axis=1 confuse almost every beginner at first. The simplest way to remember it: axis=0
moves DOWN the rows (collapsing rows, giving one result per column), while axis=1 moves
ACROSS the columns (collapsing columns, giving one result per row). You will see this exact same
axis logic again in Pandas in Chapter 3.

Filtering Arrays with Conditions (Boolean Indexing)


You can filter an array using a condition directly, without writing a loop — this is the NumPy equivalent of
Excel's filter feature and SQL's WHERE clause.

11
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

sales = [Link]([1200, 1500, 980, 2200, 1750])

high_sales = sales[sales > 1500]


print(high_sales) # [2200 1750]

print(sales > 1500) # [False False False True True] -- a boolean mask

count_high = (sales > 1500).sum()


print(count_high) # 2 -- True counts as 1, False as 0

Mini Assignment
Create a NumPy array of 10 numbers representing daily website visitors for two weeks (use any
realistic numbers). Calculate the mean, median, and standard deviation. Then create a filtered array
containing only the days where visitors were above the mean.

Practice Exercise – Chapter 2


1. Create two NumPy arrays: one of product costs, one of selling prices (5 products each). Calculate
the profit array (selling price minus cost) in one line.
2. Given an array of 20 exam scores, find how many scored above 80 using boolean indexing.
3. Create a 2D array representing 3 students' scores across 4 subjects. Calculate each student's
average score (hint: use axis=1).
4. Use [Link]() to round an array of prices like [19.987, 5.111, 100.456] to 2 decimal places.

12
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PART 3
Pandas
DataFrames, filtering, sorting, GroupBy, and merging -- the analyst's core toolkit

Chapter 3: Pandas — The Data Analyst's Most


Important Tool
Pandas is the single most important Python library for data analysis. It introduces the DataFrame — a
table structure with rows and columns, exactly like an Excel sheet or a SQL table — and gives you
powerful, fast tools to filter, clean, summarize, and reshape that data. If you only learn one Python library
in this entire course, it should be Pandas.

Series and DataFrames: The Two Core Structures


Structure What It Is Excel/SQL Equivalent

Series A single labeled column of data. One column in a spreadsheet.

DataFrame A full table made of multiple Series An entire spreadsheet or SQL table.
(columns).

PYTHON

import pandas as pd

# A Series -- one column


prices = [Link]([10, 20, 30, 40])
print(prices)

# A DataFrame -- a full table, built from a dictionary of columns


data = {
"product": ["Mouse", "Chair", "Lamp", "Desk"],
"price": [10, 75, 15, 150],
"quantity": [50, 12, 30, 8]
}
df = [Link](data)
print(df)

By convention, almost every analyst names their main DataFrame variable df — short for 'DataFrame'.
You'll see this convention used everywhere, including throughout the rest of this course.

Reading Data from Files

13
The Complete Data Analyst Course Volume 3: Python for Data Analysis

In real work, you rarely type data in by hand — you load it from a file or database. Pandas can read
almost any common data format directly into a DataFrame with one line.

PYTHON

df = pd.read_csv("sales_data.csv")
df = pd.read_excel("sales_data.xlsx")
df = pd.read_json("sales_data.json")

# Reading directly from a SQL database connection (recall Volume 2):


# df = pd.read_sql("SELECT * FROM orders", connection)

Exploring a DataFrame
Before doing any analysis, always inspect a new dataset first. These commands are the very first thing
every analyst runs on any new DataFrame.

Command What It Shows

[Link]() First 5 rows (use [Link](10) for the first 10).

[Link]() Last 5 rows.

[Link] (number of rows, number of columns).

[Link]() Column names, data types, and missing-value counts.

[Link]() Mean, min, max, and other statistics for numeric columns.

[Link] List of all column names.

[Link] Data type of each column.

Tip for Beginners


Make [Link]() and [Link]() an automatic habit every single time you load a new dataset. Together
they tell you the column names, what the data actually looks like, the data types, and whether any
values are missing — all in two lines, before you write a single line of real analysis.

Selecting Columns and Rows

14
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Selecting Columns

PYTHON

df["price"] # Select one column (returns a Series)


df[["product", "price"]] # Select multiple columns (returns a DataFrame)

Selecting Rows with .loc and .iloc


Method Selects By Example

.loc[] Label / condition [Link][df['price'] > 50]

.iloc[] Integer position [Link][0] (first row)

PYTHON

[Link][0] # First row, by position


[Link][0:3] # First 3 rows

[Link][0] # Row with index label 0


[Link][df['price'] > 50] # All rows where price is above 50 -- this is
# the Pandas equivalent of SQL's WHERE clause

Filtering Rows (The Pandas Equivalent of SQL's WHERE)


PYTHON

# Single condition
expensive = df[df["price"] > 50]

# Multiple conditions: & for AND, | for OR -- each condition needs parentheses
result = df[(df["price"] > 50) & (df["quantity"] < 20)]

# Matching a list of values (like SQL's IN)


result = df[df["product"].isin(["Mouse", "Desk"])]

Common Mistake
Beginners often write df[df["price"] > 50 and df["quantity"] < 20] using Python's normal 'and'/'or'
keywords. This causes an error or wrong results in Pandas. Always use the symbols & and | instead,
and wrap each condition in its own parentheses, exactly as shown above.

15
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Sorting Data
PYTHON

df.sort_values("price") # Ascending (smallest first)


df.sort_values("price", ascending=False) # Descending (largest first)
df.sort_values(["product", "price"]) # Sort by multiple columns

Creating and Modifying Columns


PYTHON

df["total_value"] = df["price"] * df["quantity"]

df["price_category"] = df["price"].apply(
lambda x: "Expensive" if x > 50 else "Affordable"
)

.apply() with a small inline function (called a lambda) runs custom logic on every row of a column —
this is the Pandas equivalent of Excel's IF formula and SQL's CASE WHEN, both covered in earlier
volumes.

GroupBy: Pandas' Version of SQL's GROUP BY


groupby() is one of the most powerful and frequently used Pandas features. It works exactly like SQL's
GROUP BY from Volume 2: split the data into buckets by a column's values, then summarize each
bucket.

PYTHON

# Total revenue per category


[Link]("category")["total_value"].sum()

# Multiple statistics at once


[Link]("category")["total_value"].agg(["sum", "mean", "count"])

# Group by multiple columns


[Link](["category", "region"])["total_value"].sum()

16
The Complete Data Analyst Course Volume 3: Python for Data Analysis

TIP: Why This Matters


Every business summary you learned to write in SQL in Volume 2 (revenue per region, orders per
customer, average order value per month) can be written almost identically in Pandas using
groupby(). Recognizing this parallel makes Pandas far less intimidating — you already know the
thinking, you're just learning new syntax for it.

Merging DataFrames (Pandas' Version of SQL JOIN)


merge() combines two DataFrames using a shared key column — directly equivalent to the JOIN types
from Volume 2, Chapter 3.

PYTHON

orders = [Link]({
"order_id": [1, 2, 3],
"customer_id": [101, 102, 101]
})
customers = [Link]({
"customer_id": [101, 102],
"customer_name": ["Ali", "Sara"]
})

merged = [Link](orders, customers, on="customer_id", how="inner")


print(merged)

how= value Equivalent SQL JOIN

inner INNER JOIN

left LEFT JOIN

right RIGHT JOIN

outer FULL OUTER JOIN

Mini Assignment
Create two small DataFrames by hand: one for products (product_id, product_name, price) and one
for sales (sale_id, product_id, quantity_sold). Merge them using an inner join, then create a new
column for total revenue per sale, then use groupby() to find total revenue per product.

17
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Practice Exercise – Chapter 3


1. Load any small dataset of your choice into a DataFrame (or build one with at least 8 rows by
hand) and run [Link](), [Link](), and [Link]().
2. Filter the DataFrame for rows matching two conditions combined with &.
3. Use groupby() to summarize one numeric column by one categorical column, showing sum,
mean, and count together.
4. Create a new column using .apply() with a lambda that labels each row based on a condition.

18
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PART 4
Data Cleaning
Missing values, duplicates, data types, text cleaning, and outliers

Chapter 4: Data Cleaning with Pandas


Real-world data is almost never clean. Missing values, duplicate rows, inconsistent text formatting, and
wrong data types are the norm, not the exception. Industry surveys consistently find that data analysts
spend 60–80% of their time cleaning data before any real analysis begins — this chapter covers the
exact tools you'll use constantly.

Finding Missing Values


PYTHON

[Link]() # True/False grid showing where values are missing


[Link]().sum() # Count of missing values per column -- the most
# commonly used version of this command
[Link]().sum().sum() # Total missing values in the entire DataFrame

Handling Missing Values


Method What It Does

[Link]() Removes any row that has at least one missing value.

[Link](subset=['price']) Removes rows only where 'price' specifically is missing.

[Link](0) Replaces every missing value with 0.

df['price'].fillna(df['price'].m Replaces missing prices with the column's average -- a


ean()) common, defensible default.

[Link](method='ffill') Fills missing values using the previous row's value ('forward
fill').

19
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

print([Link]().sum())

df_clean = [Link](subset=["customer_id"]) # Drop rows missing a required field


df["price"] = df["price"].fillna(df["price"].mean()) # Fill missing prices with the
average

Common Mistake
Calling [Link]() with no arguments drops a row if even ONE column anywhere in that row has a
missing value, which can silently delete far more data than intended on a wide table. Always check
[Link]().sum() first, and use the subset= parameter to target only the columns that actually matter
for your analysis.

Removing Duplicate Rows


PYTHON

[Link]() # True/False for each row -- True means it's a repeat


[Link]().sum() # Count how many duplicate rows exist

df_clean = df.drop_duplicates() # Remove exact duplicate rows


df_clean = df.drop_duplicates(subset=["customer_id"]) # Treat rows as duplicates
based
# on only this column

Fixing Data Types


Data loaded from CSV files often comes in as the wrong type — a date column read as plain text, or a
number stored as text because of a stray symbol. Always check [Link] after loading any new file.

PYTHON

df["order_date"] = pd.to_datetime(df["order_date"])
df["price"] = df["price"].astype(float)
df["customer_id"] = df["customer_id"].astype(str)

Cleaning Text Columns


String methods in Pandas (accessed through .str) let you clean text columns across an entire column at
once, without a loop — mirroring Excel's text functions from Volume 1.

20
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

df["city"] = df["city"].[Link]() # Remove extra leading/trailing spaces


df["city"] = df["city"].[Link]() # Convert to lowercase for consistency
df["city"] = df["city"].[Link]() # Capitalize each word: 'lahore' -> 'Lahore'
df["city"] = df["city"].[Link]("Lhr", "Lahore") # Fix a known
typo/abbreviation

TIP: Why This Matters


Inconsistent text is one of the most common real-world data problems: 'Lahore', 'lahore', and
'LAHORE ' might all represent the same city but will be treated as three different groups by
groupby() until cleaned. Always standardize case and whitespace on text columns before grouping
or filtering on them.

Renaming and Dropping Columns


PYTHON

df = [Link](columns={"qty": "quantity", "cust_id": "customer_id"})

df = [Link](columns=["unused_column"])
df = [Link](columns=["col1", "col2"]) # Drop multiple columns at once

Detecting and Handling Outliers


An outlier is a value far outside the normal range of your data — often a data entry error, but sometimes
a genuine extreme case worth investigating rather than deleting. A common, defensible method uses the
Interquartile Range (IQR), which you'll cover formally in Volume 4 (Statistics).

PYTHON

Q1 = df["price"].quantile(0.25)
Q3 = df["price"].quantile(0.75)
IQR = Q3 - Q1

lower_bound = Q1 - 1.5 * IQR


upper_bound = Q3 + 1.5 * IQR

outliers = df[(df["price"] < lower_bound) | (df["price"] > upper_bound)]


df_no_outliers = df[(df["price"] >= lower_bound) & (df["price"] <= upper_bound)]

21
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Tip for Beginners


Never delete outliers automatically without looking at them first. An unusually large order might be a
data entry mistake (someone typed an extra zero) — or it might be your single biggest customer.
Always investigate before deciding whether to fix, remove, or keep an outlier.

A Complete Data Cleaning Workflow


Here is a realistic, complete cleaning sequence you'll repeat, in some form, at the start of almost every
real analysis project:

PYTHON

df = pd.read_csv("raw_sales_data.csv")

print([Link])
print([Link]().sum())
print([Link]().sum())

df = df.drop_duplicates()
df = [Link](subset=["order_id", "customer_id"])
df["price"] = df["price"].fillna(df["price"].mean())
df["order_date"] = pd.to_datetime(df["order_date"])
df["city"] = df["city"].[Link]().[Link]()

print([Link]) # Compare to the original shape to see what changed

Mini Assignment
Take any small dataset (or build one with at least 15 rows by hand, deliberately including a few
missing values, a duplicate row, and inconsistent text casing in one column). Run the complete
cleaning workflow above on it, printing [Link] before and after to confirm what changed at each
step.

Practice Exercise – Chapter 4


1. Given a DataFrame with a 'quantity' column containing some missing values, fill them with 0
instead of dropping the rows.
2. Find and remove duplicate rows based only on an 'order_id' column.
3. Clean a 'product_name' text column by stripping whitespace and converting to title case.
4. Use the IQR method to identify outliers in a 'price' column and print just the outlier rows.

22
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PART 5
Data Visualization
Charts and plots with Matplotlib and Seaborn

Chapter 5: Data Visualization with Matplotlib &


Seaborn
A chart often communicates a finding faster than any table of numbers ever could. Matplotlib is Python's
foundational plotting library — flexible but a little verbose. Seaborn is built on top of Matplotlib and
produces more attractive statistical charts with far less code. Most analysts use both together: Seaborn
for quick, good-looking charts, Matplotlib to fine-tune the details.

Setting Up
PYTHON

import [Link] as plt


import seaborn as sns

sns.set_style("whitegrid") # A clean default style used throughout this chapter

Line Charts: Showing Change Over Time


Use a line chart whenever you're showing how a number changes across a continuous sequence —
almost always time (days, months, years).

PYTHON

months = ["Jan", "Feb", "Mar", "Apr", "May"]


revenue = [12000, 15000, 9800, 22000, 17500]

[Link](figsize=(8, 5))
[Link](months, revenue, marker="o", color="#2E5EAA")
[Link]("Monthly Revenue")
[Link]("Month")
[Link]("Revenue ($)")
[Link]()

Bar Charts: Comparing Categories

23
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Use a bar chart to compare a number across distinct categories — regions, products, departments.

PYTHON

categories = ["Electronics", "Furniture", "Stationery"]


sales = [45000, 32000, 8000]

[Link](figsize=(8, 5))
[Link](x=categories, y=sales, color="#2E5EAA")
[Link]("Sales by Category")
[Link]("Category")
[Link]("Total Sales ($)")
[Link]()

Using a DataFrame directly with Seaborn is even more common in real work, since you usually already
have your data grouped using groupby() from Chapter 3:

PYTHON

category_totals = [Link]("category")["total_value"].sum().reset_index()

[Link](data=category_totals, x="category", y="total_value")


[Link]()

Histograms: Understanding Distribution


A histogram shows how often values fall into different ranges — essential for understanding the overall
shape and spread of a numeric column, a concept you'll formalize in Volume 4 (Statistics).

PYTHON

[Link](figsize=(8, 5))
[Link](df["price"], bins=20, color="#2E5EAA")
[Link]("Distribution of Product Prices")
[Link]("Price")
[Link]()

Scatter Plots: Relationships Between Two Numbers


A scatter plot reveals whether (and how) two numeric variables relate to each other — for example, does
spending more on advertising actually correlate with higher sales?

24
The Complete Data Analyst Course Volume 3: Python for Data Analysis

PYTHON

[Link](figsize=(8, 5))
[Link](data=df, x="advertising_spend", y="sales")
[Link]("Advertising Spend vs Sales")
[Link]()

Box Plots: Comparing Distributions Across Categories


A box plot shows the median, spread, and outliers of a numeric column, broken down by category — an
excellent way to visually combine the GroupBy thinking from Chapter 3 with the outlier detection ideas
from Chapter 4.

PYTHON

[Link](figsize=(8, 5))
[Link](data=df, x="category", y="price")
[Link]("Price Distribution by Category")
[Link]()

Heatmaps: Visualizing a Grid of Numbers


Heatmaps are most often used to visualize correlation — how strongly pairs of numeric columns move
together (covered formally as a statistical concept in Volume 4).

PYTHON

correlation_matrix = df[["price", "quantity", "total_value"]].corr()

[Link](figsize=(6, 5))
[Link](correlation_matrix, annot=True, cmap="Blues")
[Link]("Correlation Between Numeric Columns")
[Link]()

25
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Choosing the Right Chart


Chart Type Best For

Line chart Change over time (trends).

Bar chart Comparing totals across categories.

Histogram Understanding the distribution/spread of one numeric column.

Scatter plot Relationship between two numeric columns.

Box plot Comparing distributions and spotting outliers across categories.

Heatmap Correlation between several numeric columns at once.

Common Mistake
Picking a chart type that doesn't match the question is one of the most common analyst mistakes —
for example, using a line chart for unrelated categories (like products), which implies a trend or order
that doesn't actually exist. Always ask 'what relationship am I trying to show?' before picking a chart
type, using the table above as a starting guide.

Customizing and Saving Charts


PYTHON

[Link](figsize=(10, 6))
[Link](data=category_totals, x="category", y="total_value", color="#2E5EAA")
[Link]("Sales by Category", fontsize=14, fontweight="bold")
[Link]("Category")
[Link]("Total Sales ($)")
[Link](rotation=45)
plt.tight_layout()
[Link]("sales_by_category.png", dpi=300)
[Link]()

dpi=300 produces a high-resolution image suitable for presentations and reports — always save charts
this way rather than taking a screenshot, which looks blurry by comparison.

Mini Assignment
Using the sample sales DataFrame from earlier chapters (or any small dataset of your own), create
one of each: a bar chart of total revenue by category, a histogram of prices, and a scatter plot of
price vs quantity. Add a clear title and axis labels to each, then save all three as PNG files using
dpi=300.

26
The Complete Data Analyst Course Volume 3: Python for Data Analysis

Practice Exercise – Chapter 5


1. Create a line chart showing 6 months of sales data with proper title and axis labels.
2. Create a bar chart comparing average order value across 4 different regions.
3. Create a box plot comparing a numeric column across at least 3 categories, and identify which
category has the most outliers just by looking at the chart.
4. Create a correlation heatmap for any DataFrame with at least 3 numeric columns.

Chapter Summary: What You Can Now Do


You have now covered the complete core of Python for data analysis: Python fundamentals (variables,
loops, functions), fast numerical computing with NumPy, the full Pandas workflow (loading, exploring,
filtering, sorting, grouping, and merging data), real-world data cleaning techniques, and creating
professional charts with Matplotlib and Seaborn. Combined with Excel (Volume 1) and SQL (Volume 2),
you now hold the three core technical skill sets requested in the overwhelming majority of real Data
Analyst job postings worldwide.

TIP: Keep Going


Volume 4 continues this course with Statistics for Data Analysis — descriptive statistics, probability
basics, and hypothesis testing, explained for working analysts rather than mathematicians, built with
the same complete, textbook-style depth used in this volume. Just say the word when you're ready.

27

You might also like