24
Often you might be interested in converting a pandas DataFrame to a JSON format.
Fortunately this is easy to do using the to_json() function, which allows you to convert a DataFrame to a JSON string with one of the following formats:
- ‘split’ : dict like {‘index’ -> [index], ‘columns’ ->
, ‘data’ -> [values]}
- ‘records’ : list like [{column -> value}, … , {column -> value}]
- ‘index’ : dict like {index -> {column -> value}}
- ‘columns’ : dict like {column -> {index -> value}}
- ‘values’ : just the values array
- ‘table’ : dict like {‘schema’: {schema}, ‘data’: {data}}
This tutorial shows how to convert a DataFrame to each of the six formats using the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 19], 'assists': [5, 7, 7, 12]}) #view DataFrame df points assists 0 25 5 1 12 7 2 15 7 3 19 12
Method 1: ‘Split’
df.to_json(orient='split') { "columns": [ "points", "assists" ], "index": [ 0, 1, 2, 3 ], "data": [ [ 25, 5 ], [ 12, 7 ], [ 15, 7 ], [ 19, 12 ] ] }
Method 2: ‘Records’
df.to_json(orient='records') [ { "points": 25, "assists": 5 }, { "points": 12, "assists": 7 }, { "points": 15, "assists": 7 }, { "points": 19, "assists": 12 } ]
Method 3: ‘Index’
df.to_json(orient='index') { "0": { "points": 25, "assists": 5 }, "1": { "points": 12, "assists": 7 }, "2": { "points": 15, "assists": 7 }, "3": { "points": 19, "assists": 12 } }
Method 4: ‘Columns’
df.to_json(orient='columns') { "points": { "0": 25, "1": 12, "2": 15, "3": 19 }, "assists": { "0": 5, "1": 7, "2": 7, "3": 12 } }
Method 5: ‘Values’
df.to_json(orient='values') [ [ 25, 5 ], [ 12, 7 ], [ 15, 7 ], [ 19, 12 ] ]
Method 6: ‘Table’
df.to_json(orient='table') { "schema": { "fields": [ { "name": "index", "type": "integer" }, { "name": "points", "type": "integer" }, { "name": "assists", "type": "integer" } ], "primaryKey": [ "index" ], "pandas_version": "0.20.0" }, "data": [ { "index": 0, "points": 25, "assists": 5 }, { "index": 1, "points": 12, "assists": 7 }, { "index": 2, "points": 15, "assists": 7 }, { "index": 3, "points": 19, "assists": 12 } ] }
How to Export a JSON File
You can use the following syntax to export a JSON file to a specific file path on your computer:
#create JSON file json_file = df.to_json(orient='records') #export JSON file with open('my_data.json', 'w') as f: f.write(json_file)
You can find the complete documentation for the pandas to_json() function here.