Creating a Pandas DataFrame from a JSON file

Along with CSV, JSON is another commonly found format for datasets, especially when extracting data from web APIs.

How to do it…

  1. To create a Pandas DataFrame from a JSON file, first import the Python libraries that you need:
    import pandas as pd
  2. Next, define a variable for the JSON file and enter the full path to the file:
    customer_json_file = 'customer_data.json'
  3. Next, create a DataFrame from the JSON file using the read_json() method provided by Pandas. Note that the dates in our JSON file are stored in the ISO format, so we're going to tell the read_json() method to convert dates:
    customers_json = pd.read_json(customer_json_file,
       convert_dates=True)
  4. Finally, use the head() command to see the top five rows ...

Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.