How to convert pandas DataFrame into JSON in Python?

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 DataFrames 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 single data entry and each row is unique data entry. Whereas JSON is a text written in JavaScript Object notations.

Note: For more information, refer to Python | Pandas DataFrame

Convert pandas DataFrame into JSON

To convert pandas DataFrames to JSON format we use the function DataFrame.to_json() from the pandas 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

Parameters:



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 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, 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’.
indent integer value Length of whitespace used to indent each record. Optional argument need not be mentioned.

We now look at a few examples to understand the usage of the function DataFrame.to_json.

Example 1: Basic usage

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


Output :

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

Example 2: Exploring the ‘orient’ attribute of DataFrame.to_json function

filter_none

edit
close

play_arrow

link
brightness_4
code

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")

chevron_right


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”}]}




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.