Created
December 18, 2014 18:23
-
-
Save walkerke/7d055b109a3f4b790127 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:704d1a84e5ad2101223208d8f9a386e8367ff1cb14e755340aa9c65c5357f3a4" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Testing out dygraphs in Python..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import pandas as pd\n", | |
"import wbdata as wb\n", | |
"import pydygraphs" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"over65ind = {\"SP.POP.65UP.TO.ZS\": \"pctover65\"}\n", | |
"\n", | |
"countries = [\"IT\", \"NL\", \"ES\"]\n", | |
"\n", | |
"df_all = wb.get_dataframe(over65ind, country = countries, data_date = (pd.datetime(1960, 1, 1), pd.datetime(2012, 1, 1)))\n", | |
"\n", | |
"df_all = df_all.reset_index()\n", | |
"\n", | |
"df_all.head()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>country</th>\n", | |
" <th>date</th>\n", | |
" <th>pctover65</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td> Spain</td>\n", | |
" <td> 2012</td>\n", | |
" <td> 17.510246</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td> Spain</td>\n", | |
" <td> 2011</td>\n", | |
" <td> 17.286287</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td> Spain</td>\n", | |
" <td> 2010</td>\n", | |
" <td> 17.104019</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td> Spain</td>\n", | |
" <td> 2009</td>\n", | |
" <td> 16.963305</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td> Spain</td>\n", | |
" <td> 2008</td>\n", | |
" <td> 16.857691</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": [ | |
" country date pctover65\n", | |
"0 Spain 2012 17.510246\n", | |
"1 Spain 2011 17.286287\n", | |
"2 Spain 2010 17.104019\n", | |
"3 Spain 2009 16.963305\n", | |
"4 Spain 2008 16.857691" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now we pivot the data frame to wide format..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"wide = pd.pivot_table(df_all, values = 'pctover65', index = 'date', columns = 'country').reset_index()\n", | |
"\n", | |
"wide['date'] = wide['date'].astype(float)\n", | |
"\n", | |
"wide.head()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th>country</th>\n", | |
" <th>date</th>\n", | |
" <th>Italy</th>\n", | |
" <th>Netherlands</th>\n", | |
" <th>Spain</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td> 1960</td>\n", | |
" <td> 9.512792</td>\n", | |
" <td> 8.931693</td>\n", | |
" <td> 8.201297</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td> 1961</td>\n", | |
" <td> 9.637497</td>\n", | |
" <td> 9.049868</td>\n", | |
" <td> 8.299286</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td> 1962</td>\n", | |
" <td> 9.756697</td>\n", | |
" <td> 9.168340</td>\n", | |
" <td> 8.405020</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td> 1963</td>\n", | |
" <td> 9.876381</td>\n", | |
" <td> 9.287049</td>\n", | |
" <td> 8.521242</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td> 1964</td>\n", | |
" <td> 10.004657</td>\n", | |
" <td> 9.405879</td>\n", | |
" <td> 8.651111</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"country date Italy Netherlands Spain\n", | |
"0 1960 9.512792 8.931693 8.201297\n", | |
"1 1961 9.637497 9.049868 8.299286\n", | |
"2 1962 9.756697 9.168340 8.405020\n", | |
"3 1963 9.876381 9.287049 8.521242\n", | |
"4 1964 10.004657 9.405879 8.651111" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Time to make the plot!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"## Dataframe example\n", | |
"fig2 = pydygraphs.figure()\n", | |
"\n", | |
"xaxis = 'date'\n", | |
"\n", | |
"fig2.plotDataFrame(wide, xaxis)\n", | |
"fig2.title(\"Percent of population over age 65\")\n", | |
"fig2.xlabel(\"Year\")\n", | |
"fig2.ylabel(\"Percent over 65\")\n", | |
"\n", | |
"fig2.show()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"\n", | |
" <script src=\"http://dygraphs.com/dygraph-combined.js\"></script>\n", | |
" <table style=\"width: 1050px; border-style: hidden;\">\n", | |
" <tr><td style=\"border-style: hidden;\"><div id='Figure0' style=\"width: 1050px; height: 400px;\"></div></td></tr>\n", | |
" <tr><td style=\"border-style: hidden;\"><div style=\"text-align:right; width: 1050px; height: auto;\"; id='Figure0_legend'></div></td></tr>\n", | |
" </table>\n", | |
" " | |
], | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"<IPython.core.display.HTML at 0xacfb240>" | |
] | |
}, | |
{ | |
"html": [ | |
"\n", | |
"\t <script type=\"text/javascript\">\n", | |
"\t function convertToDataTable_Figure0(d) {\n", | |
"\t var columns = _.keys(d);\n", | |
"\t var x_col = 'date';\n", | |
"\t columns.splice(columns.indexOf(x_col), 1); // Get index column. (prob index). Don't need to do this just to plot all\n", | |
"\t var out = [];\n", | |
"\t var i = 0;\n", | |
"\t for (var k in d[x_col]) {\n", | |
"\t var row = [d[x_col][k]];\n", | |
"\t columns.forEach(function(col) {\n", | |
"\t row.push(d[col][k]);\n", | |
"\t });\n", | |
"\t out.push(row);\n", | |
"\t }\n", | |
"\t return {data:out, labels:[x_col].concat(columns)};\n", | |
"\t }\n", | |
"\n", | |
"\t function handle_output_Figure0(out) {\n", | |
"\t var json = out.content.data['text/plain'];\n", | |
"\t var data = JSON.parse(eval(json));\n", | |
"\t var tabular = convertToDataTable_Figure0(data);\n", | |
"\t \n", | |
" g = new Dygraph(document.getElementById('Figure0'), tabular.data, {\n", | |
" legend: 'always',\n", | |
" labels: tabular.labels,\n", | |
" labelsDivStyles: { 'textAlign': 'right' },\n", | |
" rollPeriod: 1,\n", | |
" showRoller: true,\n", | |
" animatedZooms: true,\n", | |
"\t \n", | |
" showRoller: true,\n", | |
" \n", | |
" title: 'Percent of population over age 65',\n", | |
" xlabel: 'Year',\n", | |
" \n", | |
" ylabel: 'Percent over 65',\n", | |
" \n", | |
" labelsDiv: 'Figure0_legend',\n", | |
"\t errorBars: false\n", | |
"\t })\n", | |
"\t }\n", | |
"\t var kernel = IPython.notebook.kernel;\n", | |
"\t var callbacks_Figure0 = { 'iopub' : {'output' : handle_output_Figure0}};\n", | |
"\t kernel.execute(\"pydygraphs.__PYDYGRAPH__FIGURE__JSON__[0]\", callbacks_Figure0, {silent:false});\n", | |
"\t </script>\n", | |
"\t " | |
], | |
"metadata": {}, | |
"output_type": "display_data", | |
"text": [ | |
"<IPython.core.display.HTML at 0xac0e0f0>" | |
] | |
} | |
], | |
"prompt_number": 4 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment