Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Read JSON Files with Pandas?

  • Last Updated : 22 Nov, 2021

In this article, we are going to see how to read JSON Files with Pandas.

Reading JSON Files using Pandas

To read the files, we use read_json() function and through it, we pass the path to the JSON file we want to read. Once we do that, it returns a “DataFrame”( A table of rows and columns) that stores data. If we want to read a file that is located on remote servers then we pass the link to its location instead of a local path.

Example 1: Reading JSON file

Python3




import pandas as pd
  
  
df = pd.read_json("FILE_JSON.json")
df.head()


Output:

   One  Two
0   60  110
1   60  117
2   60  103
3   45  109
4   45  117
5   60  102

Example 2: Creating JSON data and reading in dataframe

Here we will create JSON data and then create a dataframe through it using pd.Dataframe() methods.

Python3




import pandas as pd
  
data = {
    "One": {
        "0": 60,
        "1": 60,
        "2": 60,
        "3": 45,
        "4": 45,
        "5": 60
    },
    "Two": {
        "0": 110,
        "1": 117,
        "2": 103,
        "3": 109,
        "4": 117,
        "5": 102
    }
}
  
df = pd.DataFrame(data)
  
print(df)


Output:

   One  Two
0   60  110
1   60  117
2   60  103
3   45  109
4   45  117
5   60  102

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!