Menu

Loading the Dataset with Pandas

Loading the Dataset with Pandas

Now that we understand the dataset structure, the next step is to load it into Python using Pandas so we can explore and process it.

The Iris dataset is available directly in Scikit-learn, which makes loading it simple without needing to download an external file.

from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()

df = pd.DataFrame(iris.data, columns=["sepal_length", "sepal_width", "petal_length", "petal_width"])
df["species"] = iris.target

print(df.head())

Code Explanation

  • load_iris() loads the built in Iris dataset directly from Scikit-learn.
  • pd.DataFrame(iris.data, columns=[...]) converts the raw numerical data into a readable table, using clean column names.
  • df["species"] = iris.target adds the target column, showing which species each row belongs to, using numbers 0, 1, and 2.
  • df.head() displays the first five rows, giving a quick preview of the loaded data.

After running this code, you should see a clean table with four feature columns and one species column, matching the structure.


Machine Learning Tutorial

AP

Abhishek Pati



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details