{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A IPython Notebook to Analyze LA Active Business Data\n", "\n", "As part of the effort to encourage data-journalism and informed activism, the city of Los Angeles partnered with Socrata to share public data on registered local businesses.\n", "\n", "https://data.lacity.org/A-Prosperous-City/Listing-of-Active-Businesses/\n", "\n", "This notebook is an attempt to show how the toolkit from the Python stack can be used for a real world data analysis that can allow an LA neighborhood resident to learn more about their community. In this example, we will be building a map of businesses and associated registration and purpose information into a browsable map.\n", "\n", "To view a fully interactive version of this Jupyter Notebook, you can set up a notebook server locally. For more information, see Jupyter's official documentation." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Make sure to install these packages before running:\n", "\n", "# pip install sodapy\n", "# pip install folium\n", "\n", "import folium\n", "\n", "from sodapy import Socrata" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:root:Requests made without an app_token will be subject to strict throttling limits.\n" ] } ], "source": [ "# Unauthenticated client only works with public data sets. Note 'None'\n", "# in place of application token, and no username or password:\n", "client = Socrata(\"data.lacity.org\", None)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# First 5000 results, returned as JSON from API / converted to Python list of\n", "# dictionaries by sodapy.\n", "results = client.get(\"ngkp-kqkn\", limit=5000, city='VENICE')\n", "\n", "client.close()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'business_name': 'CHARCOAL VENICE PARTNERS LLC',\n", " 'city': 'VENICE',\n", " 'council_district': '11',\n", " 'dba_name': 'CHARCOAL VENICE',\n", " 'location_1': {'coordinates': [-118.4627, 33.9815], 'type': 'Point'},\n", " 'location_account': '0002798053-0001-3',\n", " 'location_description': '425 WASHINGTON 90292-5213',\n", " 'location_start_date': '2015-01-18T00:00:00.000',\n", " 'mailing_address': '425 WASHINGTON BLVD',\n", " 'mailing_city': 'VENICE',\n", " 'mailing_zip_code': '90292-5213',\n", " 'naics': '722110',\n", " 'primary_naics_description': 'Full-service restaurants',\n", " 'street_address': '425 WASHINGTON BLVD',\n", " 'zip_code': '90292-5213'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Examine first record returned in the result set\n", "\n", "results[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def flatten(results):\n", " '''Flatten coordinates in nested dictionary'''\n", " new_dict = results.copy()\n", " try:\n", " coordinates = new_dict['location_1']['coordinates']\n", " except AttributeError:\n", " return\n", " new_dict.pop('location_1', None)\n", " new_dict['coordinates'] = coordinates\n", " return new_dict" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def reverse_coordinates(results):\n", " '''Reverse coordinates in flatted dictionary for Folium's use'''\n", " new_dict = results.copy()\n", " try:\n", " coordinates = results['coordinates']\n", " reversed_coordinates = coordinates[::-1]\n", " new_dict['coordinates'] = reversed_coordinates\n", " except AttributeError:\n", " return\n", " return new_dict" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create folium Map object centered around Venice, CA\n", "m = folium.Map(\n", " location=[33.9889, -118.4717],\n", " zoom_start=17)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Iterate through list of businesses in returned data set\n", "# and place a marker on the map with select metadata\n", "for business in results:\n", " try:\n", " data = reverse_coordinates(flatten(business))\n", " info_html_string = ' {business} d.b.a. {dba} - {description}'.format(business=data['business_name']\n", " ,description=data['primary_naics_description']\n", " ,dba=data['dba_name'])\n", " info_html_string = info_html_string.replace(\"'\", r\"\\'\")\n", " folium.Marker(data['coordinates'],popup=info_html_string).add_to(m)\n", " except KeyError:\n", " pass" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Save the output of folium.Map object to an HTML file\n", "\n", "m.save('venice_map.html')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }