Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame([['p', 'q'], ['r', 's']],
                  index=['row 1', 'row 2'],
                  columns=['c1', 'c2'])
df.to_json(orient='split')
Out[2]:
'{"columns":["c1","c2"],"index":["row 1","row 2"],"data":[["p","q"],["r","s"]]}'

Pandas: Dataframe - to_json

Encoding/decoding a Dataframe using 'records' formatted JSON:

In [3]:
df.to_json(orient='records')
Out[3]:
'[{"c1":"p","c2":"q"},{"c1":"r","c2":"s"}]'

Pandas: Dataframe - Encoding/decoding a Dataframe using 'records' formatted JSON

Encoding/decoding a Dataframe using 'index' formatted JSON:

In [4]:
df.to_json(orient='index')
Out[4]:
'{"row 1":{"c1":"p","c2":"q"},"row 2":{"c1":"r","c2":"s"}}'

Pandas: Dataframe - Encoding/decoding a Dataframe using 'index' formatted JSON

Encoding/decoding a Dataframe using 'columns' formatted JSON:

In [5]:
df.to_json(orient='columns')
Out[5]:
'{"c1":{"row 1":"p","row 2":"r"},"c2":{"row 1":"q","row 2":"s"}}'

Pandas: Dataframe - Encoding/decoding a Dataframe using 'columns' formatted JSON

Encoding/decoding a Dataframe using 'values' formatted JSON:

In [6]:
df.to_json(orient='values')
Out[6]:
'[["p","q"],["r","s"]]'

Pandas: Dataframe - Encoding/decoding a Dataframe using 'values' formatted JSON

Encoding with Table Schema:

In [7]:
df.to_json(orient='table')
Out[7]:
'{"schema": {"fields":[{"name":"index","type":"string"},{"name":"c1","type":"string"},{"name":"c2","type":"string"}],"primaryKey":["index"],"pandas_version":"0.20.0"}, "data": [{"index":"row 1","c1":"p","c2":"q"},{"index":"row 2","c1":"r","c2":"s"}]}'