{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Let's visualise data with Folium library!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Folium -> https://github.com/python-visualization/folium__" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##The dataset: Severe Acute Malnutrition (SAM) in Sahel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Dataset__: https://data.hdx.rwlabs.org/dataset/prevalence-of-severe-acute-malnutrition-of-sahel\n", "\n", "__Description__: \n", "The dataset represents the latest Severe Acute Malnutrition Prevalence available from 2010 to 2015 for the Sahel nine countries (Burkina Faso, Cameroon, Chad, the Gambia, Mali, Mauritania, Niger, Nigeria, Senegal)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Note__: More on SAM here: http://globalnutritionreport.org/2014/08/13/measuring-the-burden-of-severe-acute-malnutrition/" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from pandas import read_excel\n", "\n", "import xlrd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df_sahel = read_excel(\"Sahel_Countries_SAM_Nutrition_Admin1_Summary_Latest_Surveys.xlsx\")" ] }, { "cell_type": "code", "execution_count": 4, "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", "
CNTRY_NAMECNTRY_CODEADM1_NAMEADM1_CODERowcacode1SeasonYearSurvey nameCommentsLatest SAM Prevalence
0Burkina FasoBFABoucle du MouhounBFA046BFA046Post-Harvest2014Nutrition Survey SMART - Preliminary resultsData collection Sept-Oct 2014 (Attention P/T a...1.4
1Burkina FasoBFACascadesBFA047BFA047Post-Harvest2014Nutrition Survey SMART - Preliminary resultsData collection Sept-Oct 2014 (Attention P/T a...0.2
2Burkina FasoBFACentreBFA013BFA013Post-Harvest2013Nutrition Survey SMARTData collection Septembre 2013 (Attention P/T ...1.8
3Burkina FasoBFACentre-EstBFA048BFA048Post-Harvest2014Nutrition Survey SMART - Preliminary resultsData collection Sept-Oct 2014 (Attention P/T a...1.0
4Burkina FasoBFACentre-NordBFA049BFA049Post-Harvest2014Nutrition Survey SMART - Preliminary resultsData collection Sept-Oct 2014 (Attention P/T a...1.0
\n", "
" ], "text/plain": [ " CNTRY_NAME CNTRY_CODE ADM1_NAME ADM1_CODE Rowcacode1 \\\n", "0 Burkina Faso BFA Boucle du Mouhoun BFA046 BFA046 \n", "1 Burkina Faso BFA Cascades BFA047 BFA047 \n", "2 Burkina Faso BFA Centre BFA013 BFA013 \n", "3 Burkina Faso BFA Centre-Est BFA048 BFA048 \n", "4 Burkina Faso BFA Centre-Nord BFA049 BFA049 \n", "\n", " Season Year Survey name \\\n", "0 Post-Harvest 2014 Nutrition Survey SMART - Preliminary results \n", "1 Post-Harvest 2014 Nutrition Survey SMART - Preliminary results \n", "2 Post-Harvest 2013 Nutrition Survey SMART \n", "3 Post-Harvest 2014 Nutrition Survey SMART - Preliminary results \n", "4 Post-Harvest 2014 Nutrition Survey SMART - Preliminary results \n", "\n", " Comments Latest SAM Prevalence \n", "0 Data collection Sept-Oct 2014 (Attention P/T a... 1.4 \n", "1 Data collection Sept-Oct 2014 (Attention P/T a... 0.2 \n", "2 Data collection Septembre 2013 (Attention P/T ... 1.8 \n", "3 Data collection Sept-Oct 2014 (Attention P/T a... 1.0 \n", "4 Data collection Sept-Oct 2014 (Attention P/T a... 1.0 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_sahel.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[u'Boucle du Mouhoun Burkina Faso',\n", " u'Cascades Burkina Faso',\n", " u'Centre Burkina Faso',\n", " u'Centre-Est Burkina Faso',\n", " u'Centre-Nord Burkina Faso']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sahel_locations = [\" \".join([df_sahel[\"ADM1_NAME\"][i], df_sahel[\"CNTRY_NAME\"][i]]) for i in range(len(df_sahel))]\n", "\n", "sahel_locations[:5]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "## I need coordinates -> let's geocode with GeoPy!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from geopy.geocoders import Nominatim\n", "\n", "g = Nominatim()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sahel_coord = [g.geocode(l) for l in sahel_locations]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'latitude'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcoord\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlatitude\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlongitude\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0ml\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msahel_coord\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mcoord\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'latitude'" ] } ], "source": [ "coord = [[l.latitude, l.longitude] for l in sahel_coord]\n", "\n", "coord[:5]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ennedi Est Chad\n", "Ennedi Ouest Chad\n", "Hodh Ech Chargi Mauritania\n", "Akwa lbom Nigeria\n" ] } ], "source": [ "for i in range(len(sahel_coord)):\n", " if sahel_coord[i] is None:\n", " print(sahel_locations[i])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "coord_list = []\n", "\n", "#For the regions that weren't geocoded by geopy I looked up the coordinates on Google maps\n", "\n", "for i in range(len(sahel_coord)):\n", " if sahel_coord[i] is not None:\n", " coord_list.append((sahel_coord[i].latitude, sahel_coord[i].longitude))\n", " else:\n", " if sahel_locations[i] == \"Ennedi Est Chad\":\n", " coord_list.append((16.3074676, 22.46964))\n", " elif sahel_locations[i] == \"Ennedi Ouest Chad\":\n", " coord_list.append((18.3247397, 19.250896))\n", " elif sahel_locations[i] == \"Hodh Ech Chargi Mauritania\":\n", " coord_list.append((19.2697034, -9.4929445))\n", " elif sahel_locations[i] == \"Akwa lbom Nigeria\":\n", " coord_list.append((5.0047492, 7.619581))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(12.4777805, -3.58795703101245),\n", " (10.30718125, -4.4351892203504),\n", " (12.36894, -1.5435073646143),\n", " (11.7337, -0.289661956172839),\n", " (13.23871, -1.03498305140845)]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coord_list[:5]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{u'Burkina Faso',\n", " u'Cameroon',\n", " u'Chad',\n", " u'Gambia',\n", " u'Mali',\n", " u'Mauritania',\n", " u'Niger',\n", " u'Nigeria',\n", " u'Senegal'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(df_sahel[\"CNTRY_NAME\"])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [], "source": [ "map_sahel = folium.Map(location=(17.145087, 3.223233), zoom_start=4)\n", "\n", "for i in range(len(coord_list)):\n", " if df_sahel[\"CNTRY_NAME\"][i] == \"Mali\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"cadetblue\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Mauritania\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"purple\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Niger\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"darkpurple\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Senegal\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"darkred\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Chad\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"darkblue\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Cameroon\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"darkgreen\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Burkina Faso\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"gray\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Gambia\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"green\")\n", " elif df_sahel[\"CNTRY_NAME\"][i] == \"Nigeria\":\n", " map_sahel.simple_marker(coord_list[i], popup=\"Prevalence of SAM: %.2f\" % df_sahel[\"Latest SAM Prevalence\"][i],\n", " marker_color=\"lightgray\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Prevalence of Severe Acute Malnutrition in Sahel (2013/2014)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map_sahel" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "map_sahel.create_map(path='sam_sahel_map.html')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding a Choropleth map with administrative boundaries" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Adding Admin. bounderies of level 1 (https://data.hdx.rwlabs.org/dataset/sahel-administrative-boundaries)\n", "sahel_admin1 = \"sahel_admin1.geojson\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sahel_test = folium.Map(location=(17.145087, 3.223233), zoom_start=4.5)\n", "\n", "sahel_test.geo_json(geo_path = sahel_admin1)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sahel_test" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sahel_choro = folium.Map(location=(17.145087, 3.223233), zoom_start=4.5)\n", "\n", "sahel_choro.geo_json(geo_path = sahel_admin1,\n", " data = df_sahel,\n", " columns = [\"ADM1_CODE\", \"Latest SAM Prevalence\"],\n", " key_on='feature.properties.ADM1_CODE',\n", " fill_color = 'YlOrRd', \n", " threshold_scale = [0, 0.5, 1, 1.5, 2],\n", " fill_opacity = 0.7, \n", " line_opacity = 0.6,\n", " legend_name = 'Prevalence of SAM per admin. regions in Sahel',\n", " reset=True)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sahel_choro" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sahel_choro.create_map(path='sam_sahel_choromap.html')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }