To export Pandas DataFrame to a JSON file:
df.to_json(r"Path to store the exported JSON file\File Name.json")
Steps
Step 1: Create a DataFrame
Imagine that you have the following DataFrame that contains data about 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 this DataFrame:
Product Price
0 Computer 1200
1 Printer 200
2 Monitor 500
3 Tablet 350
4 Keyboard 80
Step 2: Export the DataFrame to a JSON File
To export Pandas DataFrame to a JSON file:
df.to_json(r"Path to store the exported JSON file\File Name.json")
For example, assume that the path where the JSON file will be exported is:
Therefore, the complete code to export the DataFrame to a JSON file is as follows:
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\my_data.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’ll 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, to get a “split” orientation, set the orient=”split”:
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\my_data.json", orient="split")
The result:
{"columns":["Product","Price"],"index":[0,1,2,3,4],"data":[["Computer",1200],["Printer",200],["Monitor",500],["Tablet",350],["Keyboard",80]]}
Here are the results 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}}
Refer to the Pandas Documentation to see the different formatting options that you can apply.