How To Read JSON Data Using Python Pandas
Reading json data in Python is very easy. Json data can be read from a file or it could be a json web link. Let us first try to read the json from a web link.
Let us first import the necessary packages "requests and pandas".
import requests
import pandas as pd
How to read json data from web link
Let us import the covid19 timeseries data from json link pomber.github.io/covid19/timeseries.json using requests
data = requests.get('https://pomber.github.io/covid19/timeseries.json')
Let us check the type of our data type.
type(data)
To get the json type, we need to use data.json() method.
jsondata = data.json()
Let us check the length of our json data.
len(jsondata)
Let us check the data type of jsondata.
type(jsondata)
Ok, it is python dictionary. Lets check the keys of our dict.
keys = jsondata.keys()
for key in keys:
print(key)
break
Ok looks like the keys are the country names. Lets check the first row.
jsondata['Afghanistan'][0]
Ok now we know our data a little better. Let us construct a dataframe from our json data.
How to convert Json to Pandas dataframe
The easiest way is to just use pd.DataFrame.from_dict method. Let us try it and see what we get.
df = pd.DataFrame.from_dict(jsondata)
df.head(1)
Ok we got the dataframe but not in the form that we wanted. We will have to unwind the nested data to build a proper dataframe.
columns=['country','date','confirmed','deaths','recovered']
data = []
for country in jsondata:
for x in jsondata[country]:
data.append([country, x['date'],x['confirmed'],x['deaths'],x['recovered']])
df = pd.DataFrame(data,columns=columns)
As we see above, we had to loop through dictionary of dictionaries. Let us see our dataframe now.
df.head(2)
Ok this looks good now. Now we can perform all the regular dataframe methods on our data.
Related Notebooks
- How To Read CSV File Using Python PySpark
- How To Analyze Wikipedia Data Tables Using Python Pandas
- How to Visualize Data Using Python - Matplotlib
- Json Python
- How To Analyze Data Using Pyspark RDD
- How to Analyze the CSV data in Pandas
- Pandas Read and Write Excel File
- How to do SQL Select and Where Using Python Pandas
- JSON Parse Error Syntax Error Unexpected token N In JSON