Remove index from dataframe before converting to json with split orientation

Since two years back pandas (>= v0.23.0) offers an index argument (only valid for orient='split' and orient='table'):

df = pd.DataFrame([[1, 2], [3, 4]], ['x', 'y'], ['a', 'b'])
df.to_json(orient='split', index=True)
# '{"columns":["a","b"],"index":["x","y"],"data":[[1,2],[3,4]]}'
df.to_json(orient='split', index=False)
# '{"columns":["a","b"],"data":[[1,2],[3,4]]}'

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html


  • import json module
  • Convert to json with to_json(orient='split')
  • Use the json module to load that string to a dictionary
  • Delete the index key with del json_dict['index']
  • Convert the dictionary back to json with json.dump or json.dumps

Demo

import json

df = pd.DataFrame([[1, 2], [3, 4]], ['x', 'y'], ['a', 'b'])

json_dict = json.loads(df.to_json(orient='split'))
del json_dict['index']
json.dumps(json_dict)

'{"columns": ["a", "b"], "data": [[1, 2], [3, 4]]}'