Skip to content

Instantly share code, notes, and snippets.

@jazzido

jazzido/US Visas.ipynb

Last active Mar 16, 2017
Embed
What would you like to do?
US Visas Issued to Argentines
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Nonimmigrant Visa Issuances by Visa Class and by Nationality\n",
"\n",
"Thanks to [Jeremy Singer-Vine](https://twitter.com/jsvine)'s fantastic [Data is Plural](https://tinyletter.com/data-is-plural/letters/data-is-plural-2017-03-15-edition) newsletter for the link to this dataset."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <script>\n",
" \n",
" function vct_load_lib(url, callback){\n",
" if(typeof d3 !== 'undefined' &&\n",
" url === 'http://d3js.org/d3.v3.min.js'){\n",
" callback()\n",
" }\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = true;\n",
" s.onreadystatechange = s.onload = callback;\n",
" s.onerror = function(){\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" };\n",
" var vincent_event = new CustomEvent(\n",
" \"vincent_libs_loaded\",\n",
" {bubbles: true, cancelable: true}\n",
" );\n",
" \n",
" function load_all_libs(){\n",
" console.log('Loading Vincent libs...')\n",
" vct_load_lib('http://d3js.org/d3.v3.min.js', function(){\n",
" vct_load_lib('http://d3js.org/d3.geo.projection.v0.min.js', function(){\n",
" vct_load_lib('http://wrobstory.github.io/d3-cloud/d3.layout.cloud.js', function(){\n",
" vct_load_lib('http://wrobstory.github.io/vega/vega.v1.3.3.js', function(){\n",
" window.dispatchEvent(vincent_event);\n",
" });\n",
" });\n",
" });\n",
" });\n",
" };\n",
" if(typeof define === \"function\" && define.amd){\n",
" if (window['d3'] === undefined ||\n",
" window['topojson'] === undefined){\n",
" require.config(\n",
" {paths: {\n",
" d3: 'http://d3js.org/d3.v3.min',\n",
" topojson: 'http://d3js.org/topojson.v1.min'\n",
" }\n",
" }\n",
" );\n",
" require([\"d3\"], function(d3){\n",
" console.log('Loading Vincent from require.js...')\n",
" window.d3 = d3;\n",
" require([\"topojson\"], function(topojson){\n",
" window.topojson = topojson;\n",
" load_all_libs();\n",
" });\n",
" });\n",
" } else {\n",
" load_all_libs();\n",
" };\n",
" }else{\n",
" console.log('Require.js not found, loading manually...')\n",
" load_all_libs();\n",
" };\n",
"\n",
" </script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"import vincent\n",
"vincent.core.initialize_notebook()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data = pd.read_excel(\"https://travel.state.gov/content/dam/visas/Statistics/Non-Immigrant-Statistics/NIVDetailTables/FYs97-16_NIVDetailTable.xls\", sheetname=None)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"COUNTRY = 'Argentina'\n",
"rv = pd.DataFrame()\n",
"for s in data.keys():\n",
" df = data[s]\n",
" year = df.columns[0][-4:]\n",
" x = df[df[df.columns[0]] == COUNTRY]\n",
" x.loc[:,'year'] = year\n",
" rv = rv.append(x)\n",
"\n",
"rv.sort_values('year', inplace=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Draw chart"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div id=\"vis2419c405d4e5464ab1dffa039a1ac239\"></div>\n",
"<script>\n",
" ( function() {\n",
" var _do_plot = function() {\n",
" if (typeof vg === 'undefined') {\n",
" window.addEventListener('vincent_libs_loaded', _do_plot)\n",
" return;\n",
" }\n",
" vg.parse.spec({\"axes\": [{\"scale\": \"x\", \"title\": \"A\\u00f1o\", \"type\": \"x\"}, {\"scale\": \"y\", \"title\": \"Cantidad\", \"type\": \"y\"}], \"data\": [{\"name\": \"table\", \"values\": [{\"col\": \"F-1\", \"idx\": \"1997\", \"val\": 3040.0}, {\"col\": \"F-1\", \"idx\": \"1998\", \"val\": 3108.0}, {\"col\": \"F-1\", \"idx\": \"1999\", \"val\": 3446.0}, {\"col\": \"F-1\", \"idx\": \"2000\", \"val\": 3208.0}, {\"col\": \"F-1\", \"idx\": \"2001\", \"val\": 3170.0}, {\"col\": \"F-1\", \"idx\": \"2002\", \"val\": 1885.0}, {\"col\": \"F-1\", \"idx\": \"2003\", \"val\": 1350.0}, {\"col\": \"F-1\", \"idx\": \"2004\", \"val\": 1153.0}, {\"col\": \"F-1\", \"idx\": \"2005\", \"val\": 1075.0}, {\"col\": \"F-1\", \"idx\": \"2006\", \"val\": 1115.0}, {\"col\": \"F-1\", \"idx\": \"2007\", \"val\": 1061.0}, {\"col\": \"F-1\", \"idx\": \"2008\", \"val\": 1117.0}, {\"col\": \"F-1\", \"idx\": \"2009\", \"val\": 1076.0}, {\"col\": \"F-1\", \"idx\": \"2010\", \"val\": 1103.0}, {\"col\": \"F-1\", \"idx\": \"2011\", \"val\": 1156.0}, {\"col\": \"F-1\", \"idx\": \"2012\", \"val\": 1155.0}, {\"col\": \"F-1\", \"idx\": \"2013\", \"val\": 1248.0}, {\"col\": \"F-1\", \"idx\": \"2014\", \"val\": 1293.0}, {\"col\": \"F-1\", \"idx\": \"2015\", \"val\": 1450.0}, {\"col\": \"F-1\", \"idx\": \"2016\", \"val\": 1482.0}, {\"col\": \"H-1B\", \"idx\": \"1997\", \"val\": 535.0}, {\"col\": \"H-1B\", \"idx\": \"1998\", \"val\": 560.0}, {\"col\": \"H-1B\", \"idx\": \"1999\", \"val\": 627.0}, {\"col\": \"H-1B\", \"idx\": \"2000\", \"val\": 817.0}, {\"col\": \"H-1B\", \"idx\": \"2001\", \"val\": 1106.0}, {\"col\": \"H-1B\", \"idx\": \"2002\", \"val\": 1644.0}, {\"col\": \"H-1B\", \"idx\": \"2003\", \"val\": 1517.0}, {\"col\": \"H-1B\", \"idx\": \"2004\", \"val\": 1507.0}, {\"col\": \"H-1B\", \"idx\": \"2005\", \"val\": 1517.0}, {\"col\": \"H-1B\", \"idx\": \"2006\", \"val\": 1406.0}, {\"col\": \"H-1B\", \"idx\": \"2007\", \"val\": 1185.0}, {\"col\": \"H-1B\", \"idx\": \"2008\", \"val\": 859.0}, {\"col\": \"H-1B\", \"idx\": \"2009\", \"val\": 753.0}, {\"col\": \"H-1B\", \"idx\": \"2010\", \"val\": 727.0}, {\"col\": \"H-1B\", \"idx\": \"2011\", \"val\": 675.0}, {\"col\": \"H-1B\", \"idx\": \"2012\", \"val\": 525.0}, {\"col\": \"H-1B\", \"idx\": \"2013\", \"val\": 487.0}, {\"col\": \"H-1B\", \"idx\": \"2014\", \"val\": 434.0}, {\"col\": \"H-1B\", \"idx\": \"2015\", \"val\": 399.0}, {\"col\": \"H-1B\", \"idx\": \"2016\", \"val\": 336.0}, {\"col\": \"J-1\", \"idx\": \"1997\", \"val\": 1233.0}, {\"col\": \"J-1\", \"idx\": \"1998\", \"val\": 1514.0}, {\"col\": \"J-1\", \"idx\": \"1999\", \"val\": 1695.0}, {\"col\": \"J-1\", \"idx\": \"2000\", \"val\": 2063.0}, {\"col\": \"J-1\", \"idx\": \"2001\", \"val\": 2534.0}, {\"col\": \"J-1\", \"idx\": \"2002\", \"val\": 2209.0}, {\"col\": \"J-1\", \"idx\": \"2003\", \"val\": 2818.0}, {\"col\": \"J-1\", \"idx\": \"2004\", \"val\": 3031.0}, {\"col\": \"J-1\", \"idx\": \"2005\", \"val\": 3645.0}, {\"col\": \"J-1\", \"idx\": \"2006\", \"val\": 3933.0}, {\"col\": \"J-1\", \"idx\": \"2007\", \"val\": 4746.0}, {\"col\": \"J-1\", \"idx\": \"2008\", \"val\": 4603.0}, {\"col\": \"J-1\", \"idx\": \"2009\", \"val\": 4947.0}, {\"col\": \"J-1\", \"idx\": \"2010\", \"val\": 3369.0}, {\"col\": \"J-1\", \"idx\": \"2011\", \"val\": 3989.0}, {\"col\": \"J-1\", \"idx\": \"2012\", \"val\": 3435.0}, {\"col\": \"J-1\", \"idx\": \"2013\", \"val\": 3333.0}, {\"col\": \"J-1\", \"idx\": \"2014\", \"val\": 3836.0}, {\"col\": \"J-1\", \"idx\": \"2015\", \"val\": 3729.0}, {\"col\": \"J-1\", \"idx\": \"2016\", \"val\": 4393.0}]}, {\"name\": \"stats\", \"source\": \"table\", \"transform\": [{\"keys\": [\"data.idx\"], \"type\": \"facet\"}, {\"type\": \"stats\", \"value\": \"data.val\"}]}], \"height\": 500, \"legends\": [{\"fill\": \"color\", \"offset\": 0, \"properties\": {}, \"title\": \"Categories\"}], \"marks\": [{\"from\": {\"data\": \"table\", \"transform\": [{\"keys\": [\"data.col\"], \"type\": \"facet\"}, {\"height\": \"data.val\", \"point\": \"data.idx\", \"type\": \"stack\"}]}, \"marks\": [{\"properties\": {\"enter\": {\"fill\": {\"field\": \"data.col\", \"scale\": \"color\"}, \"width\": {\"band\": true, \"offset\": -1, \"scale\": \"x\"}, \"x\": {\"field\": \"data.idx\", \"scale\": \"x\"}, \"y\": {\"field\": \"y\", \"scale\": \"y\"}, \"y2\": {\"field\": \"y2\", \"scale\": \"y\"}}}, \"type\": \"rect\"}], \"type\": \"group\"}], \"padding\": \"auto\", \"scales\": [{\"domain\": {\"data\": \"table\", \"field\": \"data.idx\"}, \"name\": \"x\", \"range\": \"width\", \"type\": \"ordinal\", \"zero\": false}, {\"domain\": {\"data\": \"stats\", \"field\": \"sum\"}, \"name\": \"y\", \"nice\": true, \"range\": \"height\"}, {\"domain\": {\"data\": \"table\", \"field\": \"data.col\"}, \"name\": \"color\", \"range\": \"category20\", \"type\": \"ordinal\"}], \"width\": 960}, function(chart) {\n",
" chart({el: \"#vis2419c405d4e5464ab1dffa039a1ac239\"}).update();\n",
" });\n",
" };\n",
" _do_plot();\n",
" })();\n",
"</script>\n",
"<style>.vega canvas {width: 100%;}</style>\n",
" "
],
"text/plain": [
"<vincent.charts.Bar at 0x10b8acbd0>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CATEGORIES = ['J-1', 'F-1', 'H-1B']\n",
"\n",
"chart_data = {c: list(rv[c]) for c in CATEGORIES}\n",
"chart_data['year'] = list(rv['year'])\n",
"\n",
"stacked = vincent.StackedBar(chart_data, iter_idx='year')\n",
"stacked.axis_titles(x='Año', y='Cantidad')\n",
"stacked.legend(title='Categories')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment