How to convert pandas DataFrame into JSON in Python

In this article, we will study how to convert pandas DataFrame into JSON in Python.

DataFrame stores the data. It aligns the data in tabular fashion. Hence, it is a 2-dimensional data structure.

Let us create DataFrame. Look at the following code:

import pandas as pd

student_data = {"ID No":["S001","S002","S003","S004"],
                "Name" :["Vihaan","Ritesh","Niharika","Yash"],
                "Score":[98,78,53,90]}

df = pd.DataFrame(student_data)

print(df)

OUTPUT

  ID No      Name  Score
0  S001    Vihaan     98
1  S002    Ritesh     78
2  S003  Niharika     53
3  S004      Yash     90

JSON refers to JavaScript Object Notation. JSON stores and exchange the data. Hence, JSON is a plain text. In Python, JSON is a built in package. A JSON file is a file that stores data in JavaScript Object Notation (JSON) format. JSON is easy to understand.

Conversion of Pandas DataFrame to JSON

Let us now look how to convert pandas dataframe into JSON.

(i) Using DataFrame_name.to_json()

The to_json() function converts objects to JSON string. It’s syntax is as follow:

DataFrame_name.to_json(path=None, orient=None, double_precision=10, date_unit=’ms’, default_handler=None, force_ascii=True, lines=False, compression=’infer’,date_format=None, index=True)

  • path- It is the path of the json file. If we do not specify the path, string is obtained.
  • orient- It will indicate the string format of JSON.
  • double_precision- It is the number of decimal places to be used to encode floating point values.
  • date_unit- It is the time unit to encode.
  • default_handler- It is the handler to call. It’s default value is None.
  • force_ascii- It will force encoded string to be ASCII. It’s default value is true.
  • lines- It’s default value is False.
  • compression- It is the string value that represents the compression for the file.
  • date_format- It is a type of date conversion.
  • index- It’s value is boolean. If true, it will include index value for JSON string.

Look at the following code:

df.to_json('df_to_json.json')

OUTPUT

{ "ID No": {
                "0":"S001",
                "1":"S002",
                "2":"S003",
                "3":"S004"
           },
 
  "Name":  {
                "0":"Vihaan",
                "1":"Ritesh",
                "2":"Niharika",
                "3":"Yash"
           },
 
  "Score":{
                "0":98,
                "1":78,
                "2":53,
                "3":90
          }
}

In this example, “df” is the name of DataFrame. The function to_json() performs conversion. The converted json data is stored in the file with name “df_to_json.json”. The output will be visible in this file.

The same method can be used in another way to perform the conversion. Look at the following code:

with open('df_to_json.json', 'w') as a:
    a.write(df.to_json())

Similar to the above method, df.to_json() converts DataFrame into JSON.  The “open” function opens the file. It will take the name of the file and the mode of operation. Here, “w” refers to write. It’s output is the same as above and output will be saved in “df_to_json.json” file.

Thank You.

You may also read: How to convert DataFrame into List using Python?

Leave a Reply

Your email address will not be published. Required fields are marked *