Remove index from dataframe before converting to json with split orientation

dataframe, json, pandas, python

Solution

- 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]]}'

Problem

I am outputting a pandas dataframe to a json object using the following: ``` df_as_json = df.to_json(orient='split') ``` In the json object superfluous indexes are stored. I do no want to include these. To remove them I tried ``` df_no_index = df.to_json(orient='records') df_as_json = df_no_index.to_json(orient='split') ``` However I get a ``` AttributeError: 'str' object has no attribute 'to_json' ``` Is there a fast way to reorganize the dataframe so that is does not contain a separate index column during or prior to the .to_json(orient='split') call?

Original source