What is pandas read_json() function in Python?
Overview
Python uses an open-source package Pandas. With it, we can easily read and analyze large datasets.
The read_json() function
By using the read_json() function and importing the Pandas package, we can easily read a JSON file and display its data in a
Syntax
read_json(path)
Parameter
This method takes the parameter, path, which represents the file path or directory.
Return value
It returns JSON data as DataFrame or series.
Example
{"Names":{"0":"Amelia Jacob","1":"Samantha Kyle","2":"Isabella Elizabeth","3":"Rhys Amelia","4":"James Sarah","5":"Olivia Poppy"},"Hobbies":{"0":"NovelReading","1":"Cricket","2":"Painting","3":"Football","4":"Cooking","5":"Gaming"},"Age":{"0":25,"1":20,"2":23,"3":19,"4":23,"5":12}}
Explanation
The main.py file
- Line 1: We import the Pandas library as
pdalias. - Line 2: We return data of the file in a DataFrame. We assign a variable name,
mydataframe, in which thedata.jsonfile is loaded with the help of theread_json()function.
- Line 3: We print the resultant DataFrame on the screen. Here, we use
to_string()to display all the data present in the JSON file.
The data.json file
We can store any type of data we want to store in this file. In this case, we are storing demo data.
"Names" represents the columns of the data frame. Similarly, "Hobbies" and "Age" are also representing the other columns of the resultant data frame.
The numbers included inside the brackets {"0", "1", and so on} represent the index number. By convention, DataFrame always starts at the index 0. Therefore, the first entry will be at the 0 index.