Python · Week 4 of 4 · 6–7 hours
Pandas, DataFrames, Data Cleaning and Data Manipulation
Load a real dataset into pandas, clean it and answer questions with it.
Learning objectives
By the end of this week you will be able to:
- Load a CSV into a DataFrame and inspect it.
- Select columns and filter rows.
- Find and fix missing values, duplicates and wrong data types.
- Sort, create new columns, group and merge data.
Lessons
Pandas Basics
pandas is the main Python library for working with tables. It has two building blocks:
- A Series: one column of data with labels.
- A DataFrame: a whole table of Series side by side, like a spreadsheet in code.
import pandas as pd
df = pd.read_csv("sales.csv") # or pd.read_excel("sales.xlsx")DataFrames: Inspecting and Selecting
Always look at your data before analyzing it:
| Code | Shows |
|---|---|
df.head() | first 5 rows |
df.shape | (rows, columns) |
df.info() | column names, types and non-missing counts |
df.describe() | count, mean, min, max and quartiles of number columns |
df["Region"].value_counts() | how often each value appears |
Selecting: df["Sales"] gives one column; df[["Region","Sales"]] (double brackets) gives several. df.loc[row_label, column] selects by label and df.iloc[0:5] by position.
Filtering uses the same boolean idea as NumPy:
df[df["Sales"] > 500] df[(df["Region"] == "North") & (df["Sales"] > 500)] # and = &, or = |, wrap each test in ()
Data Cleaning in pandas
The same problems you fixed in Excel appear here, but now each fix is a line of code you can re-run on next month's file.
- Missing values:
df.isna().sum()counts them per column. Then decide:df.dropna(subset=["Sales"])removes rows missing Sales;df["Region"].fillna("Unknown")fills gaps. - Duplicates:
df.duplicated().sum()counts them;df = df.drop_duplicates()removes them. - Wrong types:
df["Date"] = pd.to_datetime(df["Date"]);df["Sales"] = pd.to_numeric(df["Sales"], errors="coerce")turns bad values into NaN instead of crashing. - Messy text:
df["Region"] = df["Region"].str.strip().str.title() - Column names:
df.columns = df.columns.str.lower().str.replace(" ", "_")
Note that most pandas methods return a new DataFrame; assign the result back (df = ...) to keep the change.
Data Manipulation
- New columns:
df["Revenue"] = df["Units"] * df["Price"] - Sorting:
df.sort_values("Revenue", ascending=False) - Grouping (like a PivotTable or SQL GROUP BY):
df.groupby("Region")["Revenue"].sum(). Several statistics at once:df.groupby("Region")["Revenue"].agg(["sum","mean","count"]) - Pivot tables:
pd.pivot_table(df, values="Revenue", index="Region", columns="Category", aggfunc="sum") - Merging (like a lookup or SQL join):
pd.merge(sales, products, on="product_id", how="left") - Dates:
df["Month"] = df["Date"].dt.monththen group by Month.
Every one of these maps onto something you already know from Excel or SQL. pandas lets you chain them together and repeat the whole analysis with one click.
Watch
- VideoComplete Pandas Tutorial - Basics to Advanced
Watch the first half on DataFrames, selecting and filtering.
Practice
Download a free sales or retail CSV (for example a "Superstore" dataset from Kaggle). Then:
- Inspect it with head, shape, info and describe.
- Count missing values and duplicates, and fix them. Convert the date column to datetime.
- Clean one text column with str.strip and str.title.
- Create a Revenue or Profit column if one does not exist.
- Find total sales by region, the top 10 products, and sales by month.
- Make a pivot table of sales by region and category.
Check your understanding
Try each question first, then open it to see the answer.
What does df.info() tell you that df.head() does not?
The data type and number of non-missing values in every column.
How do you filter rows where Region is West and Sales is over 1000?
df[(df["Region"] == "West") & (df["Sales"] > 1000)]
Which pandas tool is equivalent to a SQL JOIN or Excel lookup?
pd.merge().
Assignment
Course project: submit a notebook that loads, cleans and analyzes a dataset. Use text cells to explain each step and end with three findings written in plain English.