The following syntax can be used to export Pandas DataFrame to a JSON file:
df.to_json(r'Path to store the exported JSON file\File Name.json')
Next, you’ll see the steps to apply this syntax in practice.
Steps to Export Pandas DataFrame to JSON
Step 1: Gather the Data
Let’s suppose that you have the following data about different products and their prices:
Product | Price |
Computer | 1200 |
Printer | 200 |
Monitor | 500 |
Tablet | 350 |
Keyboard | 80 |
The ultimate goal is to export the above data into JSON.
Step 2: Create a DataFrame
You may then use the following code to capture the data about the products and prices:
import pandas as pd data = {'Product': ['Computer', 'Printer', 'Monitor', 'Tablet', 'Keyboard'], 'Price': [1200, 200, 500, 350, 80] } df = pd.DataFrame(data) print(df)
Once you run the code in Python, you’ll get the following DataFrame:
Product Price
0 Computer 1200
1 Printer 200
2 Monitor 500
3 Tablet 350
4 Keyboard 80
Step 3: Export Pandas DataFrame to JSON File
Finally, you may use the syntax below in order to export Pandas DataFrame to a JSON file:
df.to_json(r'Path to store the exported JSON file\File Name.json')
For example, let’s assume that the path where the JSON file will be exported is as follows:
C:\Users\Ron\Desktop\export_dataframe.json
You’ll need to adjust the path (in the Python code below) to reflect the location where the JSON file will be stored on your computer:
import pandas as pd data = {'Product': ['Computer', 'Printer', 'Monitor', 'Tablet', 'Keyboard'], 'Price': [1200, 200, 500, 350, 80] } df = pd.DataFrame(data) df.to_json(r'C:\Users\Ron\Desktop\export_dataframe.json')
Run the code (adjusted to your path), and the JSON file will be stored at your specified location.
There are few ways to view the JSON content. An easy way is to drag the file created into your web browser. You should then get the following result:
{"Product":{"0":"Computer","1":"Printer","2":"Monitor","3":"Tablet","4":"Keyboard"},"Price":{"0":1200,"1":200,"2":500,"3":350,"4":80}}
Different JSON Formats
There are different ways to format the JSON string. You’ll need to set the orient to your desired format. Here are the options:
- split
- records
- index
- values
- table
- columns (the default format)
For example, this is how the general syntax would look like if you set the orient=’split’ as follows:
df.to_json(r'Path to store the JSON file\File Name.json', orient='split')
And here is the full Python code for our example:
import pandas as pd data = {'Product': ['Computer', 'Printer', 'Monitor', 'Tablet', 'Keyboard'], 'Price': [1200, 200, 500, 350, 80] } df = pd.DataFrame(data) df.to_json(r'C:\Users\Ron\Desktop\export_dataframe.json', orient='split')
Once you run the above code, you’ll get this output:
{"columns":["Product","Price"],"index":[0,1,2,3,4],"data":[["Computer",1200],["Printer",200],["Monitor",500],["Tablet",350],["Keyboard",80]]}
Here is the output that you’ll get for each of the other formats:
orient=’records’
[{"Product":"Computer","Price":1200},{"Product":"Printer","Price":200},{"Product":"Monitor","Price":500},{"Product":"Tablet","Price":350},{"Product":"Keyboard","Price":80}]
orient=’index’
{"0":{"Product":"Computer","Price":1200},"1":{"Product":"Printer","Price":200},"2":{"Product":"Monitor","Price":500},"3":{"Product":"Tablet","Price":350},"4":{"Product":"Keyboard","Price":80}}
orient=’values’
[["Computer",1200],["Printer",200],["Monitor",500],["Tablet",350],["Keyboard",80]]
orient=’table’
{"schema":{"fields":[{"name":"index","type":"integer"},{"name":"Product","type":"string"},{"name":"Price","type":"integer"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":0,"Product":"Computer","Price":1200},{"index":1,"Product":"Printer","Price":200},{"index":2,"Product":"Monitor","Price":500},{"index":3,"Product":"Tablet","Price":350},{"index":4,"Product":"Keyboard","Price":80}]}
orient=’columns’ (default)
{"Product":{"0":"Computer","1":"Printer","2":"Monitor","3":"Tablet","4":"Keyboard"},"Price":{"0":1200,"1":200,"2":500,"3":350,"4":80}}
You may refer to the Pandas Documentation to see the different formatting options that you may apply.