#!pip3 install --upgrade folium #Use the latest version of folium pulled directly from github !pip3 install --upgrade git+git://github.com/python-visualization/folium import pandas as pd import folium from IPython.display import HTML def inline_map(map): """ Embeds the HTML source of the map directly into the IPython notebook. This method will not work if the map depends on any files (json data). Also this uses the HTML5 srcdoc attribute, which may not be supported in all browsers. """ map._build_map() return HTML(''.format(srcdoc=map.HTML.replace('"', '"'))) def embed_map(map, path="map.html"): """ Embeds a linked iframe to the map into the IPython notebook. Note: this method will not capture the source of the map into the notebook. This method should work for all maps (as long as they use relative urls). """ map.create_map(path=path) return HTML(''.format(path=path)) import requests url='https://github.com/martinjc/UK-GeoJSON/blob/master/json/electoral/gb/wpc.json?raw=true' r = requests.get(url) with open("data/wpc.json", "wb") as code: code.write(r.content) r=None df=pd.read_html('http://www.electionforecast.co.uk/tables/predicted_probability_by_seat.html') df[0][:10] #If necessary, grab a local copy... (really should add a timestamp to the filename...) df[0].to_csv('data/pred.csv',index=False) tmp=pd.DataFrame({'val':[100],'const':['Aldershot']}) tmp !head data/pred.csv map = folium.Map(location=[53, 0],zoom_start=6) map.geo_json(geo_path='wpc.json', data=df[0],data_out='data.json', columns=['Seat', 'Labour'], key_on='feature.properties.PCON13NM',threshold_scale=[0, 20, 40, 60, 80, 100], fill_color='OrRd') embed_map(map) inline_map(map) wpc_long=pd.melt(df[0],id_vars=['Seat','Region','2010']) #http://stackoverflow.com/a/19818942 wpc_favourite=wpc_long.sort('value', ascending=False).groupby('Seat', as_index=False).first() wpc_favourite[:3] wpc_favourite[ wpc_favourite['Seat']=='Isle of Wight' ]