Open In App

How to convert pandas DataFrame into JSON in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

Data Analysis is an extremely important tool in today’s world. A key aspect of Data Analytics is an organized representation of data. There are numerous data structures in computer science to achieve this task. In this article, we talk about two such data structures viz. Pandas DataFrame and JSON. Further, we see how to convert DataFrames to JSON format. Pandas DataFrames are tabular representations of data where columns represent the various data points in a single data entry and each row is a unique data entry. Whereas JSON is a text written in JavaScript Object notations.

Convert Pandas DataFrame into JSON

To convert pandas DataFrames to JSON format we use the function DataFrame.to_json() from the Panda’s library in Python. There are multiple customizations available in the to_json function to achieve the desired formats of JSON. Let’s look at the parameters accepted by the functions and then explore the customization

Pandas DataFrame: to_json() function Syntax

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

Parameter :

  • path_or_buf: File path or object. If not specified, the result is returned as a string.
  • orient: Indication of expected JSON string format.
  • date_format: None, ‘epoch’, ‘iso’}
  • double_precision: The number of decimal places to use when encoding floating point values.
  • force_ascii: Force encoded string to be ASCII.
  • date_unit: string, default ‘ms’ (milliseconds)
  • default_handler: callable, default None
  • lines: bool, default False
  • compression: {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}
  • index: Bool

Return Type: JSON Object

Description of Parameters of to_json() method in Python

Parameter Value Use
path_or_buf string or filename, optional File path or object. If not specified, the result is returned as a string.
orient ‘split’, ‘records’, ‘index’, ‘columns’, ‘values’, ‘table’, default=’index’ Indication of expected JSON string format.
date_format None, ‘epoch’, ‘iso’, default=’epoch’ Type of date conversion. ‘epoch’ = epoch milliseconds, ‘iso’ = ISO8601. The default depends on the orient. For orient=’table’, the default is ‘iso’. For all other orients, the default is ‘epoch’.
double_precision integer value, default=10 The number of decimal places to use when encoding floating point values.
force_ascii boolean value, default=True Force encoded string to be ASCII.
date_unit ‘s’, ‘ms’, ‘us’, ‘ns’, default=’ms’ The time unit to encode to, governs timestamp and ISO8601 precision. The values represent second, millisecond, microsecond, and nanosecond respectively.
default_handler callable function Handler to call if an object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serializable object.
lines Boolean value, default=False If ‘orient’ is ‘records’ write out line delimited JSON format. Will throw ValueError if incorrect ‘orient’ since others are not list-like.
compression ‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None, default=’infer’ A string representing the compression to use in the output file is only used when the first argument is a filename. By default, the compression is inferred from the filename.
index Boolean value, default=True Whether to include the index values in the JSON string. Not including the index (index=False) is only supported when orient is ‘split’ or ‘table’.

Convert Dataframe to JSON Python

In this program, we are going to convert a pandas data frame to a JSON string using the to_json() method of the pandas library. Firstly we import the required library after that we have created a NumPy array and then convert this array to a data frame using the DataFrame() method of the pandas’ library. After that, we convert the data frame to a JSON object using the to_json() method of pandas in Python.

Python3




# Import required modules
import numpy as np
import pandas as pd
  
# Create an array using numpy
data = np.array([['1', '2'], ['3', '4']])
  
# Convert data array into dataframe
dataFrame = pd.DataFrame(data, columns = ['col1', 'col2'])
  
# Convert dataframe into json object
json = dataFrame.to_json()
  
# Print json object
print(json)


Output :

{"col1":{"0":"1", "1":"3"}, "col2":{"0":"2", "1":"4"}}

Exploring the ‘orient’ attribute of the DataFrame.to_json function

In the program, we created the data frame using the same method as above after that we are exploring the different attributes of ‘orient’ parameter of to_json method of Pandas in Python.

Python3




import numpy as np
import pandas as pd
    
data = np.array([['1', '2'], ['3', '4']])
     
dataFrame = pd.DataFrame(data, columns = ['col1', 'col2'])
json = dataFrame.to_json()
print(json)
    
json_split = dataFrame.to_json(orient ='split')
print("json_split = ", json_split, "\n")
     
json_records = dataFrame.to_json(orient ='records')
print("json_records = ", json_records, "\n")
     
json_index = dataFrame.to_json(orient ='index')
print("json_index = ", json_index, "\n")
     
json_columns = dataFrame.to_json(orient ='columns')
print("json_columns = ", json_columns, "\n")
     
json_values = dataFrame.to_json(orient ='values')
print("json_values = ", json_values, "\n")
     
json_table = dataFrame.to_json(orient ='table')
print("json_table = ", json_table, "\n")


Output:

json_split = {“columns”:[“col1”, “col2”], “index”:[0, 1], “data”:[[“1”, “2”], [“3”, “4”]]}
json_records = [{“col1″:”1”, “col2″:”2”}, {“col1″:”3”, “col2″:”4”}]
json_index = {“0”:{“col1″:”1”, “col2″:”2”}, “1”:{“col1″:”3”, “col2″:”4”}}
json_columns = {“col1”:{“0″:”1”, “1”:”3″}, “col2”:{“0″:”2”, “1”:”4″}}
json_values = [[“1”, “2”], [“3”, “4”]]
json_table = {“schema”:{“fields”:[{“name”:”index”, “type”:”integer”}, {“name”:”col1″, “type”:”string”}, {“name”:”col2″, “type”:”string”}], “primaryKey”:[“index”], “pandas_version”:”0.20.0″}, “data”:[{“index”:0, “col1″:”1”, “col2″:”2”}, {“index”:1, “col1″:”3”, “col2″:”4”}]}



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads