Created
May 18, 2018 19:51
-
-
Save rcarneva/89f631d32321cd839bff5049ca6a783a to your computer and use it in GitHub Desktop.
pandas-friendly enigma.io retrieval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_current_snapshot_id(dataset_id): | |
url = base_url + "datasets/" + dataset_id | |
r = requests.get(url, headers=headers) | |
dataset = r.json() | |
return dataset['current_snapshot']['id'] | |
def get_matching_rows(snapshot_id, limit=1000, search_string=None, field=None): | |
q = "&query={}".format(search_string) if search_string is not None else "" | |
url = "{base}snapshots/{ss}?row_limit={lim}{q}".format(**{'base': base_url, 'ss': snapshot_id, 'lim': limit, 'q': q}) | |
r = requests.get(url, headers=headers) | |
snapshot = r.json() | |
try: | |
rows = snapshot['table_rows']['rows'] | |
except KeyError: | |
return snapshot | |
df = pd.DataFrame(snapshot['table_rows']['rows'], columns=[f['name'] for f in snapshot['fields']]) | |
return df if field is None else df[df[field].fillna('').str.lower().str.contains(search_string.lower())] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment