Flatten nested JSONs
A feature of JSON data is that it can be nested: an attribute's value can consist of attribute-value pairs. This nested data is more useful unpacked, or flattened, into its own dataframe columns. The pandas.io.json submodule has a function, json_normalize(), that does exactly this.
The Yelp API response data is nested. Your job is to flatten out the next level of data in the coordinates and location columns.
pandas (as pd) and requests have been imported. The results of the API call are stored as response.
Latihan ini adalah bagian dari kursus
Streamlined Data Ingestion with pandas
Petunjuk latihan
- Load the
json_normalize()function frompandas'io.jsonsubmodule. - Isolate the JSON data from
responseand assign it todata. - Use
json_normalize()to flatten and load the businesses data to a dataframe,cafes. Set thesepargument to use underscores (_), rather than periods. - Show the
datahead.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Load json_normalize()
____
# Isolate the JSON data from the API response
data = ____
# Flatten business data into a dataframe, replace separator
cafes = ____(data["businesses"],
____)
# View data
print(cafes.head())