- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read a JSON file into a DataFrame using Python Pandas library?
JSON data looks very similar to a python dictionary, but JSON is a data format whereas a dictionary is a data structure. To read JSON files into pandas DataFrame we have the read_json method in the pandas library. Below examples give you the overview of how we can read JSON files into a pandas DataFrame.
Reading local JSON file into the pandas DataFrame
#importing pandas package import pandas as pd # reading JSON file df = pd.read_json('E:\iris.json') # displaying sample output df.sample(5)
In the above code, we have read the local JSON file into the df variable by using the pd.read_json method, we pass the JSON file location as a string to this method. This method will automatically convert the data in JSON files into DataFrame. Initially, we imported the pandas package as pd. Finally, In the last line, we displayed the 5 sample lines from DataFrame as output.
sepalLength sepalWidth petalLength petalWidth species 149 5.9 3.0 5.1 1.8 virginica 90 5.5 2.6 4.4 1.2 versicolor 56 6.3 3.3 4.7 1.6 versicolor 38 4.4 3.0 1.3 0.2 setosa 85 6.0 3.4 4.5 1.6 versicolor
Iris JSON data set/file is taken as input to the read_json method, this data set has 5 columns and 150 rows of data. In this output block, we only displayed a sample of 5 rows of data by using the df.sample() method. This method randomly returns the data from the DataFrame.
In this same way, we can read remote JSON data, by mentioning the remote URL in the place of the file path.
import pandas as pd data = pd.read_json( 'http://universities.hipolabs.com/search?country=United+Kingdom') print(data)
In this example, we have taken a public HTTP API that is holding data in JSON format. Here also the read_json method is used to read this remote URL JSON data.
|domains||web_pages||name||alpha _two_ code||state-province||country|
|0||[abdn.ac.UK, Aberdeen.ac.uk]||[www.abdn.ac.uk/]||University of Aberdeen||GB||NaN||United Kingdom|
|1||[aber.ac.uk]||[www.aber.ac.uk/]||University of Wales, Aberystwyth||GB||NaN||United Kingdom|
|2||[abertay.ac.uk]||[www.abertay.ac.uk/]||University of Abertay Dundee||GB||NaN||United Kingdom|
|3||[aiuniv.edu]||[www.aiuniv.edu/]||American InterContinental University - London||GB||NaN||United Kingdom|
|4||[aku.edu]||[www.aku.edu/]||Aga Khan University||GB||NaN||United Kingdom|
This output has sample 4 rows of JSON data from the read_json method, this is the data from a public URL and it has 171 rows and 6 columns.
- How to put a Pandas DataFrame into a JSON file and read it again?
- How to read JSON file in Python
- How to read the contents of a JSON file using Java?
- Python - How to write pandas dataframe to a CSV file
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- How to concrete a single Series into a string Python Pandas library?
- How to read a file into a string in Golang?
- How can we read a JSON file in Java?
- Writing a Pandas DataFrame to CSV file
- How to convert a DataFrame into a dictionary in Pandas?
- How to write a JSON string to file using the Gson library in Java?
- How to read text file into a list or array with Python?
- Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file
- How to read a file from command line using Python?
- How to read a Pandas CSV file with no header?