A NumPy array is fast but anonymous. Column 3 is just "column 3." It has no memory that it was ever "square footage." A pandas DataFrame solves exactly that problem. It's what every EDA and preprocessing example in Chapters 3-4 actually loads a dataset into.
A NumPy array is like a grid of numbers with no labels. You have to remember that column 0 means square footage. A pandas DataFrame is the same grid, but with column headers written in. It's like an actual spreadsheet that has "Square Footage" typed at the top, instead of just "column A." That's the big change. A DataFrame remembers what each column is. A NumPy array doesn't. The labels are useful for the messy work of getting data ready. They help with spotting a categorical column, or picking out rows by a condition. So this course uses one for that part. Then it converts to a plain NumPy array, right before handing the data to a model. The model itself never needs the labels, only the numbers.
A DataFrame is a labeled table. Each column has a name, like
df["sqft"], not X[:, 0]. Each column also has a data type:
numerical (int64, float64) or categorical (object, or pandas' own
category type). A single column, pulled out on its own, is a
Series — a labeled 1D array. Three operations come up again and
again, in every later chapter's Python examples: selecting a column by
name (df["price"]), selecting several columns
(df[["sqft", "bedrooms"]]), and selecting rows by a condition
(df[df["price"] > 500000]). Under the hood, a DataFrame's numerical
columns are stored the same way Section 5's NumPy arrays are.
df.values (or df.to_numpy()) hands the whole thing back as a plain
array. That's exactly the design-matrix format scikit-learn's fit
methods expect.
Numerical, categorical, or target?
Sort each DataFrame column by its role in a house-price dataset.
Loading interactive…
Worked example
Selecting a subset of a DataFrame
-
1
A DataFrame
dfhas columnssqft,bedrooms,neighborhood, andprice. Write the expression that selects only the feature columns (not the target) for every house with more than 3 bedrooms. -
2
Filter rows using a boolean condition on
bedrooms. Then select the three feature columns. Leavepriceout, since it's the target, not a feature. -
3\boxed{\texttt{df[df["bedrooms"] > 3][["sqft", "bedrooms", "neighborhood"]]}}
Chapter 4's preprocessing chapter builds directly on this pattern. It separates features from the target, before any scaling or encoding happens.
Loading interactive…
Worked example
Reading a column's dtype
-
1
A column named
zip_codeis stored as text strings, like "10001," not as a number. Should it be treated as a numerical feature, or a categorical one? -
2
A zip code looks numeric. But math on it — the average zip code, or zip code times 2 — is meaningless. Its digits just encode a location. They aren't a measurable amount.
-
3\boxed{\text{Categorical}}
Chapter 4 returns to this exact judgment call. It falls under "encoding categorical variables."
Loading interactive…
Where this goes next
Chapter 3's exploratory data analysis and Chapter 4's preprocessing both work almost entirely in DataFrame notation — grouping, filtering, and checking column dtypes — before converting to the NumPy array format Chapter 6 onward's models actually train on.
Check your understanding (counts toward your progress)
In a DataFrame df, what does df["price"] return?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Selecting a column returns every value in it, not a single summary statistic like a mean.
Selecting one named column returns just that column, not a filtered set of every numerical column.
Bracket notation with a column name is valid pandas syntax for selecting a single column -- no error occurs.
Why
df["price"] returns a Series -- a single labeled column, pulled out on its own.
What is the key difference between a pandas DataFrame and a plain NumPy array, for the purposes of this course?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
This is backwards -- a DataFrame is specifically better suited to mixed numerical/categorical data, while a bare NumPy array of mixed types loses its speed advantage.
NumPy is generally the faster option for pure numerical operations; DataFrames add overhead for the labeling they provide.
The labeling and dtype-awareness genuinely change what each structure is good for, even though a DataFrame's numeric data is stored the same way underneath.
Why
A DataFrame's defining feature is that every column has a name and a data type, letting code refer to "sqft" instead of an anonymous column index.
Explain why a machine-learning model, at the moment it actually trains, no longer needs a DataFrame's column labels.
AI grading isn't configured on this deployment.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
First mistake
Correct answer
Also accepted:
Why
Why
Column labels help a person read and prepare data correctly, but the actual fitting math (matrix operations on a design matrix) only ever touches the numeric values, which is why the last preprocessing step is always converting to a plain array.