Lesson 12: Working with Real Data — Pandas Essentials

Load CSV data into a DataFrame; explore, select, filter, and clean data.

By • Last updated • 75 min

Real-world data is messy. It's often in CSV files or SQL databases. Pandas is the ultimate tool for wrangling structured data.

Think of Pandas as Excel or Google Sheets, but running on Python code. We usually import it as pd.

The DataFrame

The core of Pandas is the DataFrame — a 2D table of data with rows and columns.

import pandas as pd

# Usually, you load data from a file:
# df = pd.read_csv("housing.csv")

# You can see the first few rows like this:
df.head()

Interactive DataFrame Explorer

Run typical Pandas commands on real Titanic passenger data and inspect the output.

Run Pandas Commands
Data Wrangling Demo

Notice the missing ages (NaN) inside our dataset. Click below to fill missing records with the column average (30).

IdxNamePclassAgeFareSurvived
0John Doe322$7.250
1Jane Smith138$71.281
2William Cook3NaN$8.050
3Anna Brown135$53.101
4Charles Case328$8.460
5Emily Stone219$26.001
6Thomas Davis3NaN$7.890
7George Vance154$51.860
>>>df

Exploring Data

Before training a model, you must understand your data.

  • df.shape - Returns (rows, columns)
  • df.info() - Column names, data types, and missing values
  • df.describe() - Summary statistics (mean, min, max) for numeric columns

Filtering and Selecting

You can grab specific columns (Features) or filter rows based on conditions.

# Select just one column:
prices = df["Price"]

# Filter rows:
expensive_homes = df[df["Price"] > 500000]

Coding Challenge: Prepping Data

Imagine we have a Pandas DataFrame called df loaded with passenger data from the Titanic. Your job is to prep the features and labels!

  1. Create a new DataFrame called features by selecting only the columns: ["Pclass", "Age", "Fare"]. (Hint: pass a list of strings inside the brackets).
  2. Create a Pandas Series called labels by selecting just the "Survived" column.
  3. The dataset has missing ages. Use features["Age"].fillna(30) to replace missing ages with 30, and assign the result back to features["Age"].
  4. Print features.head() to verify your work.
main.py
Run your code to see output here.