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 the 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. Additionally, the read_json()
function in Pandas provides various parameters to customize the reading process
Different Ways to Read JSON Using Pandas
There are various ways to read JSON using pandas or pandas to read JSON files into dataframe. here, we are discussing some generally used methods for reading JSON using pandas or pandas reading JSON files into dataframe those are following.
- Using
pd.read_json()
Method - Using
JSON
module andpd.json_normalize()
Method - Using pd.Dataframe() Methods
Create Data
Here, The code uses the Pandas library to create a DataFrame from a nested dictionary named data
, where keys “One” and “Two” correspond to columns. Each column contains values associated with numeric indices
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 } } |
Read JSON Using Pandas pd.read_json()
Method
In this example code uses `pd.read_json()` to create a DataFrame from a JSON string obtained with `json.dumps()`. The ‘orient’ parameter is set to ‘index’ to align the data properly. Finally, it prints the resulting DataFrame.
Python3
import pandas as pd # Corrected the attribute name to dumps df_read_json = pd.read_json(json.dumps(data), orient = 'index' ) print ( "DataFrame using pd.read_json() method:" ) print (df_read_json) |
Output
DataFrame using pd.read_json() method:
0 1 2 3 4 5
One 60 60 60 45 45 60
Two 110 117 103 109 117 102
Using JSON
module and pd.json_normalize()
Method
In this example code converts a Python dictionary, ‘data,’ to a JSON string, then normalizes it into a DataFrame using Pandas’ `pd.json_normalize()` method. Finally, it prints the resulting DataFrame representing the nested JSON structure.
Python3
import pandas as pd import json # Using JSON module and pd.json_normalize() method json_data = json.dumps(data) df_json_normalize = pd.json_normalize(json.loads(json_data)) print ( "\nDataFrame using JSON module and pd.json_normalize() method:" ) print (df_json_normalize) |
Output
DataFrame using pd.json_normalize() method:
One.0 One.1 One.2 One.3 One.4 One.5 Two.0 Two.1 Two.2 Two.3 \
0 60 60 60 45 45 60 110 117 103 109
Two.4 Two.5
0 117 102
Pandas Read Json File into Dataframe Using ps.Dataframe() Methods
Here we will create JSON data and then create a dataframe through it using pd.Dataframe() methods.
Python3
import pandas as pd 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