CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Streamlined Data Ingestion with pandas

Afficher le cours

Instructions

  • Load the json_normalize() function from pandas' io.json submodule.
  • Isolate the JSON data from response and assign it to data.
  • Use json_normalize() to flatten and load the businesses data to a dataframe, cafes. Set the sep argument to use underscores (_), rather than periods.
  • Show the data head.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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())
Modifier et exécuter le code