Open In App

Load JSON String into Pandas DataFrame

Let us see how can we use a dataset in JSON format in our Pandas DataFrame. This can be done using the built-in read_json() function. It enables us to read the JSON in a Pandas DataFrame.

Example : Consider the JSON file path_to_json.json :

path_to_json.json






# importing the module
import pandas
  
# reading the file
data = df.read_json("path_to_json.json")
  
# displaying the DataFrame
print(data)

Output :



Now, the final data frame also depends on the type of the JSON file. So there are mainly 3 types of orientations in JSON : 

  • Index Oriented
  • Value-Oriented 
  • Column Oriented

1. Index Oriented

This is an example of an Index Oriented JSON file. 




# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

Output :

2. Value-Oriented

This is an example of a Value-Oriented JSON file.




# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

Output :

3. Column Oriented

This is an example of a Column-oriented JSON file.




# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

Output :


Article Tags :