لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Pandas Read JSON

About JSON: JSON stands for JavaScript Object Notation, JSON is a text format for storing and transporting data.

Since the format is text only, JSON data can easily be sent between computers, and used by any programming language.

JSON is "self-describing" and easy to understand.

JSON is plain text, but has the format of an object, and is well known in the world of programming including Pandas.

How to Read JSON File In Pandas?

In our examples we will be using a JSON file called 'pulse.json'.

Example 1: Load the JSON file into a DataFrame.

Code

import pandas as pd

df = pd.read_json('pulse.json')

print(df.to_string())

the output will be


Note: to_string() is used to print the entire DataFrame.

Dictionary as JSON

JSON = Python Dictionary: JSON objects have the same format as Python dictionaries.

If your JSON code is not in a file, but in a Python Dictionary, you can load it into a DataFrame directly.

Example 2: Load a Python Dictionary into a dataframe directly

Code

import pandas as pd

data = {
  "Player":{
    "0":1,
    "1":2,
    "2":3,
    "3":4,
    "4":5,
    "5":6
  },
  "Score":{
    "0":500,
    "1":450,
    "2":300,
    "3":150,
    "4":100,
    "5":50
  },
  "Day":{
    "0":1,
    "1":2,
    "2":3,
    "3":4,
    "4":5,
    "5":6
  },
  "Rank":{
    "0":1,
    "1":2,
    "2":3,
    "3":4,
    "4":5,
    "5":6
  }
}

df = pd.DataFrame(data)

print(df) 



the output will be