Python Pandas Pandas Input Output
Read and display data from json file.
import pandas as pd
df=pd.read_json("student.json")
print(df)
Output is here
class id mark name sex
0 Four 1 75 John Deo female
1 Three 2 85 Max Ruin male
2 Three 3 55 Arnold male
3 Four 4 60 Krish Star female
4 Four 5 60 John Mike female
------------
------------
read_json(): Creating Datafram from Json string #B08
VIDEO
This will read the data from json file ( in same directory ) student.json
We can read the URL also.
import pandas as pd
df=pd.read_json("https://www.plus2net.com/php_tutorial/student.json")
print(df)
It can be your localhost also.
import pandas as pd
df=pd.read_json("http://127.0.0.1/student.json")
print(df)
Options
We can generate JSON ( to_json() )by using various orient options and while reading we can maintain the same orient option.
orient='split'
df=pd.DataFrame(data=my_dict)
df_j=df.to_json(orient='split')
print(df_j)
Output is here
{"columns":["NAME","ID","MATH","ENGLISH"],"index":[0,1,2],"data":[["Alex",1,50,50],["Ravi",2,36,48],["Ron",3,45,49]]}
To read the above JSON format, we have to maintain the same orient value as option.
df=pd.read_json(df_j,orient='split')
print(df)
orient='records'
[{"NAME":"Alex","ID":1,"MATH":50,"ENGLISH":50},{"NAME":"Ravi","ID":2,"MATH":36,"ENGLISH":48},{"NAME":"Ron","ID":3,"MATH":45,"ENGLISH":49}]
df=pd.read_json(df_j,orient='records')
orient=index
{"0":{"NAME":"Alex","ID":1,"MATH":50,"ENGLISH":50},"1":{"NAME":"Ravi","ID":2,"MATH":36,"ENGLISH":48},"2":{"NAME":"Ron","ID":3,"MATH":45,"ENGLISH":49}}
df=pd.read_json(df_j,orient='index')
orient='columns'
{"NAME":{"0":"Alex","1":"Ravi","2":"Ron"},"ID":{"0":1,"1":2,"2":3},"MATH":{"0":50,"1":36,"2":45},"ENGLISH":{"0":50,"1":48,"2":49}}
df=pd.read_json(df_j,orient='index')
orient='values'
[["Alex",1,50,50],["Ravi",2,36,48],["Ron",3,45,49]]
df=pd.read_json(df_j,orient='values')
lines
default value is False,
Read the file as a json object per line. This will work
df=pd.read_json("https://www.plus2net.com/php_tutorial/student2.json",lines=True)
Here if we use lines=False
then we will get error message.
For comparing, same data is used in two different formats.
https://www.plus2net.com/php_tutorial/student2.json
https://www.plus2net.com/php_tutorial/student.json
Python Pandas reading JSON format data from URL files and dataframes using read_json() with options
VIDEO
Questions
What is the purpose of the read_json()
function in Pandas?
How do you use the read_json()
function to read JSON data into a Pandas DataFrame?
Can the read_json()
function handle JSON files with complex nested structures?
What are the available options for the orient
parameter in the read_json()
function?
How does the orient
parameter affect the structure of the resulting DataFrame?
Can the read_json()
function read JSON data from a remote URL?
How does the lines
parameter work in the read_json()
function?
Is it possible to specify a specific subset of columns to read from the JSON data using the read_json()
function?
Can the read_json()
function handle missing or NaN values in the JSON data?
How does the convert_axes
parameter affect the orientation of the resulting DataFrame when using the read_json()
function?
« Pandas
read_csv()
to_csv()
to_excel()
« Data input and output from Pandas DataFrame
← Subscribe to our YouTube Channel here