. Advertisement .
..3..
. Advertisement .
..4..
Pandas is the most widely used Python library for visualization and data handling. It offers various functionalities and classes for efficient data reading, manipulating, and visualizing in various file formats.
The following article discusses simple methods to use Pandas read_json to read file.
What Is A JSON File?
JSON stands for JavaScript Object Notation. This data format is used to store data so that coders can read it. While it is designed for data storage, this file is also ideal for information exchange and serialization among a server and its clients.
This platform-agnostic file is a widely used format, especially in REST APIs. There are various methods to create a JSON file. Using nested dictionaries is one of the easiest ways.
Here, each item’s key is the column header while the core value lies in another dictionary with rows.
patients = {
"Name":{"0":"John","1":"Nick","2":"Ali","3":"Joseph"},
"Gender":{"0":"Male","1":"Male","2":"Female","3":"Male"},
"Nationality":{"0":"UK","1":"French","2":"USA","3":"Brazil"},
"Age" :{"0":10,"1":25,"2":35,"3":29}
}
You can also build JSON data via a dictionary list. Each item will represent a row, making it more readable and accessible than the former method.
cars = [
{"Name":"Honda", "Price": 10000, "Model":2005, "Power": 1300},
{"Name":"Toyota", "Price": 12000, "Model":2010, "Power": 1600},
{"Name":"Audi", "Price": 25000, "Model":2017, "Power": 1800},
{"Name":"Ford", "Price": 28000, "Model":2009, "Power": 1200},
]
Before using the read_json
to read a file, let’s write data to this one. In this case, you need to use the dump()
and json module.
import json
with open('E:/datasets/patients.json', 'w') as f:
json.dump(patients, f)
with open('E:/datasets/cars.json', 'w') as f:
json.dump(cars, f)
How To Use Pandas Read_JSON To Read File
Using the Pandas read_json
function to read a JSON file should never cause you any difficulty. This method returns a Dataframe containing data in column and row format. Install Pandas to utilize the function:
$ pip install pandas
Read JSON File From Local Files
In a local system directory, let’s try reading the patient.json file and store the results in the dataframe. The head()
function will print the dataframe’s header:
import pandas as pd
patients_df = pd.read_json('E:/datasets/patients.json')
patients_df.head()
Read From Remote Files
The function is not restricted to local file reading. On the other hand, it can also handle a JSON file from a remote server. All you have to do is pass the remote file’s path to the function call.
import pandas as pd
iris_data = pd.read_json("file name")
iris_data.head()
Read A JSON File’s Multiple Records
If each line comes with a record, the nrows parameter will be useful to specify the number of loading records. Yet, it can be used when you execute the lines = True
command.
# Read JSON file with records orient
df = pd.read_json('courses.json', orient='records', nrows=2, lines=True)
print(df)
Other Parameters To Read JSON
The read_json
function includes various parameters, all of which can be used for reading files:
- dtype: this one is specified with a dictionary of columns. When the command is True, the program infers it based on data. On the reverse, there are no inferred dtypes.
- convert_axes: the axes are converted to a compatible dtype.
- convert_dates: The True command converts all columns to date.
- keep_default_dates: convert the columns based on labels.
Conclusion
This article has covered how to use Pandas read_json
to read file from local to remote files.
Leave a comment