Open In App

Python Seaborn get_dataset_names() Method

Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. In this article, we will learn about the Python seaborn.get_dataset_names() Method.

What is the Seaborn get_dataset_names() Method?

The seaborn.get_dataset_names() method is used to retrieve the complete list of names of all the built-in or sample datasets provided by the seaborn library. The datasets provided by the Seaborn library are returned as a Pandas dataframe which can later be used for creating visualization or analytical reports.

Syntax of Python Seaborn get_dataset_names() Method

seaborn.get_dataset_names()

Parameters: This method doesn't take parameters.

Returns: It returns a list of names of built-in datasets in seaborn

Seaborn get_dataset_names() Method Examples

Below are examples of Python seaborn.get_dataset_names() Method:

Loading and Visualizing a Dataset

In this example, below code uses seaborn to access built-in datasets. It retrieves the list of available datasets and loads the 'iris' dataset. Finally, it displays the first few rows of the 'iris' dataset for visualization.

import seaborn as sns

# Get the list of available datasets
datasets = sns.get_dataset_names()

# Load the 'iris' dataset
iris = sns.load_dataset('iris')

print(iris.head())

Output

  sepal_length  sepal_width  petal_length  petal_width species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa
3           4.6          3.1           1.5          0.2  setosa
4           5.0          3.6           1.4          0.2  setosa

Checking Dataset Availability

In this example, below code imports seaborn and retrieves the list of built-in datasets. It checks if the 'titanic' dataset is included in the list and prints a message indicating its availability status.

import seaborn as sns

# Get the list of available datasets
datasets = sns.get_dataset_names()

if 'titanic' in datasets:
    print("The 'titanic' dataset is available.")
else:
    print("The 'titanic' dataset is not available.")

Output

The 'titanic' dataset is available.

Looping Through Available Datasets

In this example, below code imports seaborn and fetches a list of built-in datasets. It then iterates through each dataset in the list, printing out the name of each dataset.

import seaborn as sns

# Get the list of available datasets
datasets = sns.get_dataset_names()

for dataset in datasets:
    print(dataset)

Output

anagrams
anscombe
attention
brain_networks
car_crashes
diamonds
dots
dowjones
exercise
flights
fmri
geyser
glue
healthexp
iris
mpg
penguins
planets
seaice
taxis
tips
titanic
Article Tags :