How to convert data to a JSON format using pandas
Overview
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
datetimeobjects are converted into UNIX timestamps, while NaN values will be converted intonull.
Syntax
# SignatureDataFrame.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)
Parameters
path_or_buf: This can be a string, path-like object, orNone. 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.
Return value
Its return values depend on the path_or_buf argument. If path_or_buf=None, it returns false.
Example
# import librariesimport numpy as npimport pandas as pd# convert a dictionary into a dataframedf = pd.DataFrame({"brand": ["Mercedies", "Audi", "Ford"],"model": ["Benz", "A9" ,"Expedition"],"year": [2012, 2016, 2020]})# invoking to_json() to convert data frame to jsonjson = df.to_json()# print json string on consoleprint(json)
Explanation
- Line 2–3: We load the
numpyandpandaslibraries in the program. - Line 5–8: We convert a dictionary of three values into a pandas DataFrame.
- Line 10: We transform the DataFrame content into JSON format using
df.to_json(). - Line 12: We print the JSON string to the console.