This notebook shows how to pre-process the Venture capital disbursed per $1,000 of gross domestic product dataset from the National Science Foundation to create a D3 based choropleth map for states in the US.
I exported the CSV manually from the XLS file, this could also be automated using pandas.io.excel.read_excel.
The next steps are:
fips
column used as the ID column in JavaScript.import pandas as pd
import geonamescache
gc = geonamescache.GeonamesCache()
df = pd.read_csv('data/venture-capital-us-states-1998-2012-1-per-1000-dollar-gdp.csv')
print(len(df))
df.head(5)
Below I use the geonamescache package to map state names to fips codes. Note that I prepend the string US
to the fips code, as this is included in the fips property of the Admin 1 shapefile from Natural Earth Data.
states = gc.get_dataset_by_key(gc.get_us_states(), 'name')
df['fips'] = df['State'].apply(lambda x: 'US' + states[x]['fips'])
df.head(5)
df.to_csv('../static/data/csv/venture-capital-us-states.csv')
IPython Interactive Computing and Visualization Cookbook
Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython
Python Data Visualization Cookbook
Links to Amazon and Zazzle are associate links, for more info see the disclosure.
This post was written by Ramiro Gómez (@yaph) and published on May 22, 2014.