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

Update Util.py for Python3 and Pandas .20 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import requests

API_URL="http://api.censusreporter.org/1.0/data/show/{release}?table_ids={table_ids}&geo_ids={geoids}"

def get_data(tables=None, geoids=None, release='latest'):
if geoids is None:
geoids = ['040|01000US']
elif isinstance(geoids,basestring):
elif isinstance(geoids,str):
geoids = [geoids]
if tables is None:
tables = ['B01001']
elif isinstance(tables,basestring):
elif isinstance(tables,str):
tables=[tables]

url = API_URL.format(table_ids=','.join(tables).upper(),
Expand All @@ -34,6 +35,7 @@ def get_dataframe(tables=None, geoids=None, release='latest',geo_names=False,col
frame = frame.rename(columns=d)
return frame


def prep_for_pandas(json_data,include_moe=False):
"""Given a dict of dicts as they come from a Census Reporter API call, set it up to be amenable to pandas.DataFrame.from_dict"""
result = {}
Expand All @@ -51,5 +53,5 @@ def prep_for_pandas(json_data,include_moe=False):

if __name__ == '__main__':
df = get_dataframe()
print "Top 10 most populous states"
print df.sort('B01001001',ascending=False)['B01001001'].head(10)
print("Top 10 most populous states")
print(df.sort_values('B01001001',ascending=False)['B01001001'].head(10))