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