Fixing FIPS Codes Mangled by Pandas

I have been working with data produced by the U.S. Census Bureau.  They use FIPS codes to identify geographies.  When I read in the data using Pandas, the FIPS codes get converted into numbers.  After trying to force the type to string (which doesn’t work currently), I decided to create a work around.  Here it is:

def fix_fips(fips, total_length):
    """Takes a broken FIPS and repairs it"""
    fips = str(fips)
    current_length = len(fips)
    if current_length < total_length:
        number_of_leading_zeros = total_length - current_length
        leading_zeros = ''.join('0' * number_of_leading_zeros)
        fips = leading_zeros + fips
    return fips

So say I have some state level data which has a two character FIPS code read into a pandas dataframe. I would correct the mangled data by:

df['State FIPS code'] = df['State FIPS code'].apply(fix_fips, args=(2,))

Hope this helps other data ninjas out there!


Dot Density Map

I have created an interesting visualization.  It shows the density of jobs in a given sector.  Here’s an example of the visualization near where I live:

It is created with R and is fully replicable.  Interested people can check out


More Economic Data APIs

I previously posted about the FRED API which got me thinking that there are other APIs that others might not be aware of.  I would like to pass them on.

The U.S. Census Bureau has an API that give you access to economic indicators, as well as demographic data (decennial census and American Community Survey data).  I just requested my API key.  Sunlight Labs has programmed a Python wrapper but it looks to be a bit outdated.

The U.S. Bureau of Labor Statistics also has an API.  I have used the API to get unemployment rates for some widgets I’ve programmed (example).  I’m not sure if there are series that are available through the BLS that are not available through FRED.  There is also a 3rd party Python wrapper.