{ "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", " \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", "
CountryPower Consumption KWHText
0China5.523000e+12China 5,523,000,000,000
1United States3.832000e+12United 3,832,000,000,000
2European2.771000e+12European 2,771,000,000,000
3Russia1.065000e+12Russia 1,065,000,000,000
4Japan9.210000e+11Japan 921,000,000,000
\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", " \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", " \n", " \n", " \n", " \n", "
YearICPSR State CodeAlphanumeric State CodeStateVEP Total Ballots CountedVEP Highest OfficeVAP Highest OfficeTotal Ballots CountedHighest OfficeVoting-Eligible Population (VEP)Voting-Age Population (VAP)% Non-citizenPrisonProbationParoleTotal Ineligible FelonState Abv
02012411AlabamaNaN58.6%56.0%NaN2,074,3383,539,2173707440.02.6%32,23257,9938,61671,584AL
12012812Alaska58.9%58.7%55.3%301,694300,495511,792543763.03.8%5,6337,1731,88211,317AK
22012613Arizona53.0%52.6%46.5%2,323,5792,306,5594,387,9004959270.09.9%35,18872,4527,46081,048AZ
32012424Arkansas51.1%50.7%47.7%1,078,5481,069,4682,109,8472242740.03.5%14,47130,12223,37253,808AR
42012715California55.7%55.1%45.1%13,202,15813,038,54723,681,83728913129.017.4%119,455089,287208,742CA
\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 }