Import Dependencies

  • make sure to install these packages before running:
  • pip install pandas
  • pip install sodapy
In [1]:
import os
import pandas as pd
from sodapy import Socrata

# Import keys
%store -r APPTOKEN
%store -r KEYID
%store -r KEYSECRET

Get Data from Data.lacity.org

Unauthenticated Client

  • Sets throttling limits
In [2]:
# Unauthenticated client only works with public data sets. Note 'None'
# in place of application token, and no username or password:
client = Socrata("data.lacity.org", None)

# First 2000 results, returned as JSON from API / converted to Python list of
# dictionaries by sodapy.
results = client.get("6rrh-rzua", limit=2000)

# Convert to pandas DataFrame
results_df = pd.DataFrame.from_records(results)
results_df.head(2)
WARNING:root:Requests made without an app_token will be subject to strict throttling limits.
Out[2]:
location_account business_name street_address city zip_code location_description naics primary_naics_description council_district location_start_date ... :@computed_region_qz3q_ghft :@computed_region_kqwf_mjcx :@computed_region_k96s_3jcv :@computed_region_tatf_ua23 :@computed_region_2dna_qi2s mailing_address mailing_city mailing_zip_code :@computed_region_ur2y_g4cx dba_name
0 0000000108-0001-3 PALACE OF VENICE GUEST HOME /C 1727 CRENSHAW BLVD LOS ANGELES 90019-6037 1727 CRENSHAW 90019-6037 721310 Rooming & boarding houses 10 1991-05-15T00:00:00.000 ... 23080 12 648 1105 19 NaN NaN NaN NaN NaN
1 0000000115-0001-3 VINCENZO LABELLA 521 SWARTHMORE AVENUE PACIFIC PALISADES 90272-4350 521 SWARTHMORE 90272-4350 561500 Travel arrangement & reservation services 11 1990-01-01T00:00:00.000 ... NaN NaN NaN NaN NaN 521 SWARTHMORE AVENUE PACIFIC PALISADES 90272-4350 NaN NaN

2 rows × 21 columns

Authenticated Client

  • Requires authentication with tokens, you need to apply for it using documentation link below.
  • Documentation: https://dev.socrata.com/docs/app-tokens.html
  • The following is true as of 7/19/20, please check documentation in case changes have been made.

client = Socrata(data.lacity.org, MyAppToken, userame="user@example.com", password="AFakePassword")

Parameters Explained

  • Parameter 1:data.lacity.org: the website that contains the data
  • Parameter 2:MyAppToken: the App Token code assigned when request is approved.
  • Parameter 3:userame="user@example.com": Apply for the API Key and use the KEYID here.
  • Parameter 4:password="AFakePassword": Apply for the API Key and use the KEY SECRET.
In [3]:
# Example authenticated client (needed for non-public datasets):
client = Socrata("data.lacity.org",
                 APPTOKEN,
                 username=KEYID,
                 password=KEYSECRET)

# First 2000 results, returned as JSON from API / converted to Python list of
# dictionaries by sodapy.
results = client.get("6rrh-rzua", limit=2000)

# Convert to pandas DataFrame
results_df = pd.DataFrame.from_records(results)
results_df.head(5)
Out[3]:
location_account business_name street_address city zip_code location_description naics primary_naics_description council_district location_start_date ... :@computed_region_qz3q_ghft :@computed_region_kqwf_mjcx :@computed_region_k96s_3jcv :@computed_region_tatf_ua23 :@computed_region_2dna_qi2s mailing_address mailing_city mailing_zip_code :@computed_region_ur2y_g4cx dba_name
0 0000000108-0001-3 PALACE OF VENICE GUEST HOME /C 1727 CRENSHAW BLVD LOS ANGELES 90019-6037 1727 CRENSHAW 90019-6037 721310 Rooming & boarding houses 10 1991-05-15T00:00:00.000 ... 23080 12 648 1105 19 NaN NaN NaN NaN NaN
1 0000000115-0001-3 VINCENZO LABELLA 521 SWARTHMORE AVENUE PACIFIC PALISADES 90272-4350 521 SWARTHMORE 90272-4350 561500 Travel arrangement & reservation services 11 1990-01-01T00:00:00.000 ... NaN NaN NaN NaN NaN 521 SWARTHMORE AVENUE PACIFIC PALISADES 90272-4350 NaN NaN
2 0000000121-0001-9 WILCARE ECONOMIC DEVELOPMENT CORPORATION 9911 AVALON BLVD LOS ANGELES 90003-4805 9911 AVALON 90003-4805 721310 Rooming & boarding houses 8 1999-01-01T00:00:00.000 ... 22351 14 806 1176 45 448 E 99TH STREET LOS ANGELES 90003-4804 7 NaN
3 0000000132-0001-7 CARLOS ANGEL 1221 W 7TH STREET SUITE #N-111 LOS ANGELES 90017-2394 1221 7TH 90017-2394 561300 Employment services 1 1999-07-01T00:00:00.000 ... 23078 11 564 1047 76 NaN NaN NaN 54 NaN
4 0000000133-0001-1 A A OFICINA CENTRAL HISPANA DE LOS ANGELES /C 4917 S BROADWAY LOS ANGELES 90037-3211 4917 BROADWAY 90037-3211 611000 Educational services (including schools, colle... 9 1991-01-01T00:00:00.000 ... 23668 13 737 655 NaN 2607 VAN BUREN PLACE LOS ANGELES 90007-2129 7 NaN

5 rows × 21 columns