How to Load a JSON File into pandas DataFrame?

Spread the love

In this post you will learn how to read a json file into a pandas dataframe.

pandas.read_json() –

To read a json file into a pandas dataframe we use the pandas.read_json() method.

Let’s create a json file from a pandas dataframe.

import pandas as pd

data = {'Fruits': ['Apple','Banana','Orange','Mango','Grapes'],
       'Prices': [100, 20, 80, 60, 50]}
df = pd.DataFrame(data)
df

Now, let’s save this to a json file.

df.to_json('./fruits_prices.json')

if you open this file, it will look like this.

{
   "Fruits": {
      "0": "Apple",
      "1": "Banana",
      "2": "Orange",
      "3": "Mango",
      "4": "Grapes"
   },
   "Prices": {
      "0": 100,
      "1": 20,
      "2": 80,
      "3": 60,
      "4": 50
   }
}

Now, to read this json file into pandas dataframe we can use the pandas.read_json() method. All you have to do is provide the path to the json file.

df = pd.read_json('./fruits_prices.json')
df

Related Posts –

  1. How to Export a Pandas Dataframe to a JSON File ?

Rating: 1 out of 5.

Leave a Reply