Saturday, November 23, 2024
Google search engine
HomeLanguagesHow to Read JSON Files with Pandas?

How to Read JSON Files with Pandas?

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

RELATED ARTICLES

Most Popular

Recent Comments