Pandas: DataFrames for Machine Learning
Real data arrives as messy tables — mixed types, missing values, several files. A pandas DataFrame is a labeled, spreadsheet-like table that lets you load, clean, join and reshape that data until it becomes the numeric array a model can eat.
Where Pandas Sits in the ML Pipeline
Raw tabular sources become a DataFrame, get reshaped with selection, groupby and merge, then convert to a NumPy array for the model.
01.The Problem: Real Data Is a Messy Spreadsheet
You get a CSV file of customer orders.
You open it. It's a disaster:
- Half the phone numbers are blank.
- Dates are strings like "15/03/2024", not numbers.
- The customer names live in a different file.
- Your model can only eat numbers (from topic 25: a NumPy array is one dtype, no strings).
So the question becomes
How do I load, fix, join and reshape this table before it reaches the model?
That is exactly what pandas is for.
02.The Idea in Plain Words: A Named, Labeled Spreadsheet
A Series is
a one-dimensional labeled array — one column with a row label attached to each value.
A DataFrame is
a 2-D labeled collection of Series that share one index — conceptually a spreadsheet with named columns and labeled rows.
Carry this analogy through: a DataFrame is your exam-results spreadsheet. Each row is a student. Each column is a subject. The row index is the roll number. You can find any cell by saying "row Ada, column score" — that is exactly what .loc does.
The key difference from NumPy (topic 25): each column has its own dtype. So a DataFrame can hold numbers, strings, booleans, dates and missing values (NaN) side by side. That mixed-type flexibility plus the label-based index is what makes pandas ideal for real, messy data — whereas NumPy arrays stay strictly homogeneous.
import pandas as pd
df = pd.DataFrame({
"name": ["Ada", "Lin", "Raj"],
"score": [92.5, 78.0, 88.5],
"passed": [True, False, True],
})
df.head() # first rows
df.describe() # numeric summary
df.info() # dtypes + non-null counts03.Visual Intuition: The Spreadsheet Layout
Here is the DataFrame from the code above, drawn as a grid:
codeindex name score passed 0 Ada 92.5 True 1 Lin 78.0 False 2 Raj 88.5 True
- The leftmost column is the index — a label for each row (here 0, 1, 2 — but it could be names or dates).
- Each other column is a Series with its own dtype (object, float64, bool).
- Together they form the DataFrame.
Working through a tiny example: "select only the students who scored above 85."
df["score"] > 85 gives [True, False, True] — a boolean mask
The mask picks rows 0 and 2 (Ada, Raj) and drops row 1 (Lin, 78.0). That is all filtering is.
04.Selecting and Filtering
pandas offers two main accessors:
.loc— label-based: "give me the row whose label is Ada.".iloc— position-based: "give me the first row."
Plus boolean masks for filtering by condition. Getting these straight avoids the classic chained-indexing pitfalls that trigger SettingWithCopyWarning.
df["score"] # one column (Series)
df[["name", "score"]] # several columns (DataFrame)
df.loc[df["score"] > 85] # rows where score > 85
df.iloc[0:2, 0:2] # first 2 rows, first 2 columns
df.loc[df["passed"], "score"] = 100 # safe in-place assignment05.GroupBy, Aggregation and Merge
The split-apply-combine pattern underpins groupby.
Think of it like a teacher grading by class section:
- Split — separate the rows into groups by a key (e.g., "region").
- Apply — compute an aggregation on each group (e.g., mean revenue).
- Combine — stack the per-group results back into one table.
Here is a tiny example:
codeorders: groupby("region").mean(): region revenue region mean_revenue North 200 North 150 North 100 South 80 South 80
North's mean = (200 + 100) / 2 = 150. South has one row, so mean = 80.
merge joins two DataFrames on shared keys, mirroring SQL JOIN. If you have an orders table and a users table, merge glues them on user_id — just like a VLOOKUP in a spreadsheet.
These two operations cover the bulk of feature preparation on tabular data — computing per-customer averages, rolling windows, or joining a lookup table.
df.groupby("region")["revenue"].agg(["mean", "sum", "count"])
users = pd.read_csv("users.csv")
orders = pd.read_csv("orders.csv")
joined = orders.merge(users, on="user_id", how="left") # LEFT JOIN06.Missing Values and Type Coercion
Real data is full of NaN. pandas treats missingness explicitly: isna counts gaps, fillna imputes, and dropna removes rows or columns.
astype and the constructors to_datetime / to_numeric fix the string-vs-number problems that arise straight out of a CSV — e.g., turning "15/03/2024" into a real date so you can extract month or day-of-week.
This connects directly to the Data Preprocessing topic later in this phase.
Architectural Trade-offs & Production Realities
Architectural Advantages
- Intuitive labeled API close to how analysts think about tables.
- Rich I/O: reads/writes CSV, Excel, Parquet, SQL and JSON in one line.
- Handles mixed dtypes and missing data natively.
Trade-offs & Constraints
- Holds everything in memory; large data can exhaust RAM.
- Index semantics have quirks (alignment, chained assignment).
- Slower than pure NumPy/Arrow for numeric hot paths.
Data science teams routinely load events from a warehouse into a DataFrame, run groupby/merge feature derivations, then hand the numeric columns to scikit-learn as NumPy arrays or to Spark via an Arrow bridge.
Staff+ Engineering Takeaways
- A DataFrame is a labeled, mixed-dtype table of Series sharing one index.
- Prefer .loc / .iloc and boolean masks; avoid chained assignment.
- groupby implements split-apply-combine; merge reproduces SQL joins.
- Always profile missing data with isna() and coerce types on load.
- Pandas is the glue between raw tabular sources and the NumPy input a model needs.
Topic Knowledge Check
Exercise 1 of 3 • Test your architectural comprehension.
Which is the safe way to set column y to 1 for rows where x > 0?
How clear and actionable was this distributed systems breakdown?