{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Loading the Plotly Express Data Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting ready \n", "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 \n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import plotly\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5.23.0'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to do it \n", "1. Import the `plotly.express.data` submodule as `datasets`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import plotly.express.data as datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Print the names of the data sets available" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "carshare\n", "election\n", "election_geojson\n", "experiment\n", "gapminder\n", "iris\n", "medals_long\n", "medals_wide\n", "stocks\n", "tips\n", "wind\n" ] } ], "source": [ "for name in dir(datasets):\n", " if '__' not in name:\n", " print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Import one of the available data sets and check its type using the function `type`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = datasets.stocks()\n", "type(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is a pandas `DataFrame`!\n", "\n", "4. Use the method `head` to inspect the data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
dateGOOGAAPLAMZNFBNFLXMSFT
02018-01-011.0000001.0000001.0000001.0000001.0000001.000000
12018-01-081.0181721.0119431.0618810.9599681.0535261.015988
22018-01-151.0320081.0197711.0532400.9702431.0498601.020524
32018-01-221.0667830.9800571.1406761.0168581.3076811.066561
42018-01-291.0087730.9171431.1633741.0183571.2735371.040708
\n", "
" ], "text/plain": [ " date GOOG AAPL AMZN FB NFLX MSFT\n", "0 2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000\n", "1 2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988\n", "2 2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524\n", "3 2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561\n", "4 2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
dateGOOGAAPLAMZNFBNFLXMSFT
02018-01-011.0000001.0000001.0000001.0000001.0000001.000000
12018-01-081.0181721.0119431.0618810.9599681.0535261.015988
22018-01-151.0320081.0197711.0532400.9702431.0498601.020524
32018-01-221.0667830.9800571.1406761.0168581.3076811.066561
42018-01-291.0087730.9171431.1633741.0183571.2735371.040708
52018-02-050.9415280.8937711.0898680.9425211.1880090.999887
62018-02-120.9932590.9853141.1786210.9492111.3263491.043202
72018-02-191.0222821.0028571.2203650.9809471.3616361.066561
82018-02-260.9788521.0069141.2205690.9452501.4336401.055108
92018-03-051.0524481.0284571.2845490.9913301.5783611.094682
\n", "
" ], "text/plain": [ " date GOOG AAPL AMZN FB NFLX MSFT\n", "0 2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000\n", "1 2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988\n", "2 2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524\n", "3 2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561\n", "4 2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708\n", "5 2018-02-05 0.941528 0.893771 1.089868 0.942521 1.188009 0.999887\n", "6 2018-02-12 0.993259 0.985314 1.178621 0.949211 1.326349 1.043202\n", "7 2018-02-19 1.022282 1.002857 1.220365 0.980947 1.361636 1.066561\n", "8 2018-02-26 0.978852 1.006914 1.220569 0.945250 1.433640 1.055108\n", "9 2018-03-05 1.052448 1.028457 1.284549 0.991330 1.578361 1.094682" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }