Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python socrata widget #9

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# How to run
install virtualenv
install python3

create your virtual environment ```virtualenv -p python3 ~/.envs/311-data```
export your apptoken from socrata ```export SODAPY_APPTOKEN=<Your token here>```
source your virtualenv ```source ~/.envs/311-data/bin/activate```
install dependencies ```pip install -r requirements.txt```
46 changes: 46 additions & 0 deletions src/python/basicQueries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sodapy import Socrata


class BasicSodaClient(object):
def __init__(self):
matplotlib.use("TkAgg")
# If you choose to use a token, run the following command on the terminal (or add it to your .bashrc)
# $ export SODAPY_APPTOKEN=<token>
self.socrata_token = os.environ.get("SODAPY_APPTOKEN")


def get_socrata_df(self, domain, identifier):
client = Socrata(domain, self.socrata_token)
results = client.get(identifier)
df = pd.DataFrame.from_dict(results)
print(df.head())
return df


def compare_by_request(self, df_list, request_type, keys):
aggregates = [x.requesttype.str.contains(request_type).value_counts() for x in df_list]
request_type_related = pd.concat(aggregates, keys=keys, names=["Time", "Contains " + request_type], sort=True)
request_type_related = request_type_related.div(request_type_related.sum()).round(2)
request_type_related.plot(kind='barh', x="Time", y="Contains " + request_type)
print(request_type_related)
plt.show()


if __name__ == '__main__':
soda_client = BasicSodaClient()
comparisons = [ ('data.lacity.org', '2016', 'ndkd-k878'),
('data.lacity.org', '2017', 'd4vt-q4t5')]
keys = []
frames = []
for comparison in comparisons:
domain, key, identifier = comparison
df = soda_client.get_socrata_df(domain, identifier)
keys.append(key)
frames.append(df)

soda_client.compare_by_request(frames, "Bulky Items", keys)
16 changes: 16 additions & 0 deletions src/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
certifi==2019.6.16
chardet==3.0.4
cycler==0.10.0
future==0.17.1
idna==2.8
kiwisolver==1.1.0
matplotlib==3.1.1
numpy==1.16.4
pandas==0.25.0
pyparsing==2.4.1
python-dateutil==2.8.0
pytz==2019.1
requests==2.22.0
six==1.12.0
sodapy==1.5.4
urllib3==1.25.3