Menu

Creating a Pandas DataFrame

Creating a Pandas DataFrame

Machine learning datasets are easier to analyze when stored in a DataFrame.

A DataFrame provides a tabular structure with rows and columns similar to a spreadsheet.

Code

**house_price_df = pd.DataFrame(**

**housing.data,**

**columns=housing.feature_names**

**)**

Adding the Target Variable

The target variable is stored separately and must be added manually.

Code

**house_price_df["PRICE"] = housing.target**

Explanation

After this step:

  • Each row represents a housing record.
  • Each column represents a feature.
  • The final column contains the target value.

This creates a complete dataset ready for analysis.