{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"\n",
"
\n",
"___"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Choropleth Maps Exercise \n",
"\n",
"Welcome to the Choropleth Maps Exercise! In this exercise we will give you some simple datasets and ask you to create Choropleth Maps from them. Due to the Nature of Plotly we can't show you examples\n",
"\n",
"[Full Documentation Reference](https://plot.ly/python/reference/#choropleth)\n",
"\n",
"## Plotly Imports"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.graph_objs as go \n",
"from plotly.offline import init_notebook_mode,iplot\n",
"init_notebook_mode(connected=True) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Import pandas and read the csv file: 2014_World_Power_Consumption**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Check the head of the DataFrame. **"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" Country | \n",
" Power Consumption KWH | \n",
" Text | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" China | \n",
" 5.523000e+12 | \n",
" China 5,523,000,000,000 | \n",
"
\n",
" \n",
" 1 | \n",
" United States | \n",
" 3.832000e+12 | \n",
" United 3,832,000,000,000 | \n",
"
\n",
" \n",
" 2 | \n",
" European | \n",
" 2.771000e+12 | \n",
" European 2,771,000,000,000 | \n",
"
\n",
" \n",
" 3 | \n",
" Russia | \n",
" 1.065000e+12 | \n",
" Russia 1,065,000,000,000 | \n",
"
\n",
" \n",
" 4 | \n",
" Japan | \n",
" 9.210000e+11 | \n",
" Japan 921,000,000,000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Country Power Consumption KWH Text\n",
"0 China 5.523000e+12 China 5,523,000,000,000\n",
"1 United States 3.832000e+12 United 3,832,000,000,000\n",
"2 European 2.771000e+12 European 2,771,000,000,000\n",
"3 Russia 1.065000e+12 Russia 1,065,000,000,000\n",
"4 Japan 9.210000e+11 Japan 921,000,000,000"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Referencing the lecture notes, create a Choropleth Plot of the Power Consumption for Countries using the data and layout dictionary. **"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"choromap = go.Figure(data = [data],layout = layout)\n",
"iplot(choromap,validate=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## USA Choropleth\n",
"\n",
"** Import the 2012_Election_Data csv file using pandas. **"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Check the head of the DataFrame. **"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Year | \n",
" ICPSR State Code | \n",
" Alphanumeric State Code | \n",
" State | \n",
" VEP Total Ballots Counted | \n",
" VEP Highest Office | \n",
" VAP Highest Office | \n",
" Total Ballots Counted | \n",
" Highest Office | \n",
" Voting-Eligible Population (VEP) | \n",
" Voting-Age Population (VAP) | \n",
" % Non-citizen | \n",
" Prison | \n",
" Probation | \n",
" Parole | \n",
" Total Ineligible Felon | \n",
" State Abv | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2012 | \n",
" 41 | \n",
" 1 | \n",
" Alabama | \n",
" NaN | \n",
" 58.6% | \n",
" 56.0% | \n",
" NaN | \n",
" 2,074,338 | \n",
" 3,539,217 | \n",
" 3707440.0 | \n",
" 2.6% | \n",
" 32,232 | \n",
" 57,993 | \n",
" 8,616 | \n",
" 71,584 | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" 2012 | \n",
" 81 | \n",
" 2 | \n",
" Alaska | \n",
" 58.9% | \n",
" 58.7% | \n",
" 55.3% | \n",
" 301,694 | \n",
" 300,495 | \n",
" 511,792 | \n",
" 543763.0 | \n",
" 3.8% | \n",
" 5,633 | \n",
" 7,173 | \n",
" 1,882 | \n",
" 11,317 | \n",
" AK | \n",
"
\n",
" \n",
" 2 | \n",
" 2012 | \n",
" 61 | \n",
" 3 | \n",
" Arizona | \n",
" 53.0% | \n",
" 52.6% | \n",
" 46.5% | \n",
" 2,323,579 | \n",
" 2,306,559 | \n",
" 4,387,900 | \n",
" 4959270.0 | \n",
" 9.9% | \n",
" 35,188 | \n",
" 72,452 | \n",
" 7,460 | \n",
" 81,048 | \n",
" AZ | \n",
"
\n",
" \n",
" 3 | \n",
" 2012 | \n",
" 42 | \n",
" 4 | \n",
" Arkansas | \n",
" 51.1% | \n",
" 50.7% | \n",
" 47.7% | \n",
" 1,078,548 | \n",
" 1,069,468 | \n",
" 2,109,847 | \n",
" 2242740.0 | \n",
" 3.5% | \n",
" 14,471 | \n",
" 30,122 | \n",
" 23,372 | \n",
" 53,808 | \n",
" AR | \n",
"
\n",
" \n",
" 4 | \n",
" 2012 | \n",
" 71 | \n",
" 5 | \n",
" California | \n",
" 55.7% | \n",
" 55.1% | \n",
" 45.1% | \n",
" 13,202,158 | \n",
" 13,038,547 | \n",
" 23,681,837 | \n",
" 28913129.0 | \n",
" 17.4% | \n",
" 119,455 | \n",
" 0 | \n",
" 89,287 | \n",
" 208,742 | \n",
" CA | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Year ICPSR State Code Alphanumeric State Code State \\\n",
"0 2012 41 1 Alabama \n",
"1 2012 81 2 Alaska \n",
"2 2012 61 3 Arizona \n",
"3 2012 42 4 Arkansas \n",
"4 2012 71 5 California \n",
"\n",
" VEP Total Ballots Counted VEP Highest Office VAP Highest Office \\\n",
"0 NaN 58.6% 56.0% \n",
"1 58.9% 58.7% 55.3% \n",
"2 53.0% 52.6% 46.5% \n",
"3 51.1% 50.7% 47.7% \n",
"4 55.7% 55.1% 45.1% \n",
"\n",
" Total Ballots Counted Highest Office Voting-Eligible Population (VEP) \\\n",
"0 NaN 2,074,338 3,539,217 \n",
"1 301,694 300,495 511,792 \n",
"2 2,323,579 2,306,559 4,387,900 \n",
"3 1,078,548 1,069,468 2,109,847 \n",
"4 13,202,158 13,038,547 23,681,837 \n",
"\n",
" Voting-Age Population (VAP) % Non-citizen Prison Probation Parole \\\n",
"0 3707440.0 2.6% 32,232 57,993 8,616 \n",
"1 543763.0 3.8% 5,633 7,173 1,882 \n",
"2 4959270.0 9.9% 35,188 72,452 7,460 \n",
"3 2242740.0 3.5% 14,471 30,122 23,372 \n",
"4 28913129.0 17.4% 119,455 0 89,287 \n",
"\n",
" Total Ineligible Felon State Abv \n",
"0 71,584 AL \n",
"1 11,317 AK \n",
"2 81,048 AZ \n",
"3 53,808 AR \n",
"4 208,742 CA "
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Now create a plot that displays the Voting-Age Population (VAP) per state. If you later want to play around with other columns, make sure you consider their data type. VAP has already been transformed to a float for you. **"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"choromap = go.Figure(data = [data],layout = layout)\n",
"iplot(choromap,validate=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Great Job!"
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}