12

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?

3
  • have you tried orient='records'
    – Steven G
    Apr 25, 2017 at 13:08
  • This doesn't work because to_json doesn't return a pandas data frame and therefore the next call of to_json cannot work.
    – languitar
    Apr 25, 2017 at 13:17
  • @StevenG yes, this sends the column names as keys which increases file size by 30% (which I dont want to do). Apr 25, 2017 at 13:29

2 Answers 2

8
  • 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]]}'
1
  • 1
    Alternatively, go via a dictionary to start with, i.e: data = df.to_dict(orient='split') followed by del data['index']...
    – hillmark
    Jun 7, 2017 at 10:05
5

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.