› Forums › Python › Pandas (Python library) › Why Does y = home_data.SalePrice Work, but y = home_data.[features] Does Not?
- This topic is empty.
-
AuthorPosts
-
June 19, 2026 at 9:15 am #6914
When learning Pandas and Machine Learning, many beginners notice that the following code works:
y = home_data.SalePrice
but this does not:
y = home_data.[features]
Why is that?
The Big Idea
Pandas provides two different ways to access columns:
- Dot notation for a single column
- Bracket notation for one or more columns
Understanding the difference helps explain why one statement works and the other produces an error.
Single Column Access
Suppose our CSV file contains:
LotArea,YearBuilt,SalePrice 8450,2003,208500 9600,1976,181500 11250,2001,223500
After loading the file:
home_data = pd.read_csv(iowa_file_path)
we can access the
SalePricecolumn in two equivalent ways:y = home_data.SalePrice
or
y = home_data['SalePrice']
Both return the same column.
The dot notation is simply a convenient shortcut when accessing a single column.
Multiple Column Access
Now suppose we want several feature columns:
features = ['LotArea', 'YearBuilt', '1stFlrSF']
To select all of them, we use:
X = home_data[features]
which is equivalent to:
X = home_data[['LotArea', 'YearBuilt', '1stFlrSF']]
Here,
featuresis a list containing multiple column names.
Why Doesn’t
home_data.featuresWork?Consider:
features = ['LotArea', 'YearBuilt'] X = home_data.features
Pandas interprets this as:
“Find a column literally named
features.”Since no such column exists, it will fail.
The variable
featuresis a Python list, not a column name.
Why Is
home_data.[features]Invalid?This statement:
y = home_data.[features]
is not valid Python syntax.
After a dot (
.), Python expects an attribute name:home_data.SalePrice
but not:
home_data.[features]
The square brackets must be applied directly to the DataFrame:
home_data[features]
A Helpful Analogy
Think of a DataFrame as a filing cabinet.
home_data.SalePricemeans: “Give me the drawer named SalePrice.”home_data['SalePrice']means the same thing.home_data[features]means: “Give me all drawers listed in the features list.”
Python knows how to fetch one drawer using dot notation, but multiple drawers require bracket notation.
Machine Learning Convention
In many machine learning projects, you’ll see:
y = home_data['SalePrice'] features = ['LotArea', 'YearBuilt', '1stFlrSF'] X = home_data[features]
Here:
- y = target column (what we want to predict)
- X = feature columns (information used for prediction)
For house-price prediction:
Features (X) LotArea YearBuilt 1stFlrSF ↓ Machine Learning Model ↓ SalePrice (y)
Rule of Thumb
One column:
df.column_name
or
df['column_name']
Multiple columns:
df[list_of_columns]
Remember:
home_data.SalePrice ✓ home_data['SalePrice'] ✓ home_data[features] ✓ home_data.features ✗ (looks for a column named "features") home_data.[features] ✗ (invalid syntax)
Once you understand this distinction, many Pandas and Machine Learning examples become much easier to read.
-
AuthorPosts
- You must be logged in to reply to this topic.
