How to Convert Sklearn Dataset to Pandas Dataframe in Python

Sklearn datasets become handy for learning machine learning concepts. When using the sklearn datasets, you may need to convert them to pandas dataframe for manipulating and cleaning the data.

You can convert the sklearn dataset to pandas dataframe by using the pd.Dataframe(data=iris.data) method.

In this tutorial, you’ll learn how to convert sklearn datasets into pandas dataframe.

If you’re in Hurry

You can use the following code to convert the sklearn dataset to a pandas dataframe.

Code

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df["target"] = iris.target

df.head()

Dataframe Will Look Like

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20

This is how you can convert the sklearn dataset to a pandas dataframe.

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn how to convert sklearn datasets to pandas dataframe while using the sklearn datasets to create a machine learning models.

Converting Sklearn Datasets To Dataframe Without Column Names

In this section, you’ll convert the sklearn datasets to dataframes without columns names.

  • You can use this when you want to convert the dataset to a pandas dataframe for visualization purposes.
  • The columns will be named with the default indexes 0, 1, 2, 3, 4, and so on.

Code

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data)

df["target"] = iris.target

df.head()

Dataframe Will Look Like

0123target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20

Next, you’ll learn about the column names.

Converting Sklearn Datasets To Dataframe Using Feature Names As Columns

Sklearn providers the names of the features in the attribute feature_names.

  • You can use this attribute in the pd.DataFrame() method to create the dataframe with the column headers.
  • If the dataset is a classification-type dataset, then sklearn also provides the target variable for the samples in the attribute target. You can use the target to fetch the target values and append them into your dataframe

Code

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df["target"] = iris.target

df.head()

Dataframe Will Look Like

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20

This is how you can convert the sklearn dataset to pandas dataframe with column headers by using the sklearn datasets’ feature_names attribute.

Later, if you want to rename the features, you can also rename the dataframe columns.

Using Custom Column Headers

In some cases, you may need to use custom headers as columns rather than using the sklearn datasets feature_names attribute.

Code

In the following example,

  • You’ll be using the column headers only with the column names ignoring the unit of the data (cm). Here, the unit (cm) doesn’t make a big difference.
import pandas as pd

from sklearn import datasets

# Load the IRIS dataset
iris = datasets.load_iris()

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

df["target"] = iris.target

df.head()

Dataframe will Look Like

sepal_lengthsepal_widthpetal_lengthpetal_widthtarget
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20

Converting Only Specific Columns from Sklearn Dataset

In some scenarios, you may not need all the columns in the sklearn datasets to be available in the pandas dataframe.

In that case, you need to create a pandas dataframe with specific columns from the sklearn datasets.

There is no method directly available to do this. Because the sklearn datasets return a bunch of objects. You cannot retrieve a specific column from it.

Code

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df = df[["sepal_length", "petal_length"]]

df["target"] = iris.target

df.head()

Dataframe will Look Like

sepal_lengthpetal_lengthtarget
05.11.40
14.91.40
24.71.30
34.61.50
45.01.40

This is how you can convert only specific columns from the sklearn datasets to pandas dataframe.

Display Names of Target Instead Of Numbers

To display the names of the target instead of the numbers in the target column, you can use the pandas map function.

Having names in the column looks more descriptive to visualise the dataset and is easily understandable.

To map the target names to numbers after creating a dataframe:

  1. Create a dictionary with mapping for each target number with its name
  2. Apply the map() function with the dictionary on the target columns
  3. You’ll see the names of the target instead of numbers
import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df["target"] = iris.target

target_names = {0: "Iris-Setosa", 1: "Iris-Versicolour", 2:"Iris-Virginica" }

df['target'] =df['target'].map(target_names)

df.head()

Dataframe Will Look Like

The target column in the dataframe will have the actual name of the target instead of the numbers.

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.2Iris-Setosa
14.93.01.40.2Iris-Setosa
24.73.21.30.2Iris-Setosa
34.63.11.50.2Iris-Setosa
45.03.61.40.2Iris-Setosa

Conclusion

To summarize, you’ve learned how to convert the sklearn dataset to a pandas dataframe. This is the same for all the datasets you use such as

  • Boston house prices dataset
  • Iris plants dataset
  • Diabetes dataset
  • Linnerrud dataset
  • Wine recognition dataset
  • Breast cancer dataset
  • The Olivetti faces dataset
  • California Housing dataset

If you’ve any questions, comment below.

You May Also Like

4 thoughts on “How to Convert Sklearn Dataset to Pandas Dataframe in Python”

  1. I found this blog to be very simple, easy to understand, and to the point. I would like to thank you for writing this. Hope you write more blogs like this.

    Reply
  2. Thanks for the explanation, however I’d like to know how can I display the names of the class of the target instead of numbers? Right now the target column is in the form of numeric data 0,1,2 corresponding to Iris-Setosa, Iris-Versicolour, – Iris-Virginica respectively. I want to see these names instead of the numeric value using pd.DataFrame. Is it possible?

    Reply

Leave a Comment