8. Loading the Plotly Express Data Sets#

Plotly express comes with several data sets pre-loaded which we can use for testing and exploration purposes. In this recipe we will learn to load such data sets.

Getting ready#

  1. Import the plotly module and cerify that your version is 4.14 (this is when the data sets were included for the first time) or newer

import plotly
plotly.__version__
'6.0.0'

How to do it#

  1. Import the plotly.express.data submodule as datasets

import plotly.express.data as datasets
  1. Print the names of the data sets available

for name in dir(datasets):
    if '__' not in name:
        print(name)
AVAILABLE_BACKENDS
BACKENDS_WITH_INDEX_SUPPORT
carshare
election
election_geojson
experiment
gapminder
import_module
iris
medals_long
medals_wide
nw
os
stocks
tips
wind
  1. Import one of the available data sets and check its type using the function type

data = datasets.stocks()
type(data)
pandas.core.frame.DataFrame

It is a pandas DataFrame!

  1. Use the method head to inspect the data

data.head()
date GOOG AAPL AMZN FB NFLX MSFT
0 2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
1 2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988
2 2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524
3 2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561
4 2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708

Notice that the head method receives an argument n (which defaults to 5) which represents the number of rows of the DataFrame to be show. Let’s display the first 10 rows by calling head(10)

data.head(10)
date GOOG AAPL AMZN FB NFLX MSFT
0 2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
1 2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988
2 2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524
3 2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561
4 2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708
5 2018-02-05 0.941528 0.893771 1.089868 0.942521 1.188009 0.999887
6 2018-02-12 0.993259 0.985314 1.178621 0.949211 1.326349 1.043202
7 2018-02-19 1.022282 1.002857 1.220365 0.980947 1.361636 1.066561
8 2018-02-26 0.978852 1.006914 1.220569 0.945250 1.433640 1.055108
9 2018-03-05 1.052448 1.028457 1.284549 0.991330 1.578361 1.094682