In Python, pandas is specifically designed for data manipulation to assist in data analysis. It offers many built-in data structures and operations to manipulate time series and tabular data. Similarly, DataFrame is a data structure just like tables, but it allows us to perform multiple operations on data.
We use to_json()
to convert a DataFrame or Series to a valid
Note: The
datetime
objects are converted into UNIX timestamps, while NaN values will be converted intonull
.
# Signature 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, indent=None, storage_options=None)
path_or_buf
: This can be a string, path-like object, or None
. It shows which type of results are returned by this function.orient
: This shows the standard JSON format. It can be index, column, split, records, values, or table.date_format
: This indicates the date conversion format. It can be epoch or iso.Its return values depend on the path_or_buf
argument. If path_or_buf=None
, it returns false
.
# import libraries import numpy as np import pandas as pd # convert a dictionary into a dataframe df = pd.DataFrame({ "brand": ["Mercedies", "Audi", "Ford"], "model": ["Benz", "A9" ,"Expedition"], "year": [2012, 2016, 2020]}) # invoking to_json() to convert data frame to json json = df.to_json() # print json string on console print(json)
numpy
and pandas
libraries in the program.df.to_json()
.RELATED TAGS
CONTRIBUTOR
View all Courses