TransWikia.com

Is there an easy way to convert this API get request into a DataFrame?

Stack Overflow Asked by Andrew Hicks on November 17, 2021

I am trying to get this US Census Bureau api data get request into a dataframe and thought that it was a list of list but is showing up as a NoneType. Is there a way to make this into a dataframe that could be easily exported into a CSV file?

import request

# The Basic API Request:

# Build base URL
HOST = "https://api.census.gov/data"
year = "2010"
dataset = "dec/sf1"
base_url = "/".join([HOST, year, dataset])

# Specify Census variables and other predicates
get_vars = ["NAME","P013001","P037001"]
predicates = {}
predicates["get"] = ",".join(get_vars)
predicates["for"] = "state:*"

# Execute the request, examine text of response object
data = requests.get(base_url, params=predicates)
print(data.text)

This does produce the following output:

[["NAME","P013001","P037001","state"],
["Alabama","37.9","3.02","01"],
["Alaska","33.8","3.21","02"],
["Arizona","35.9","3.19","04"],
...
["Wyoming","36.8","2.96","56"],
["Puerto Rico","36.9","3.17","72"]]

One Answer

The data.text is a string, so you could parse it through json, try this

import json
import pandas as pd 
data = pd.DataFrame(json.loads(data.text)[1:], columns=['NAME', 'P013001', 'P037001', 'state'])

and you'll get something similar to the image below. enter image description here

Answered by Miguel Trejo on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP