Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove index from dataframe before converting to json with split orientation

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?

like image 419
proximacentauri Avatar asked Apr 25 '17 13:04

proximacentauri


People also ask

How do you remove an index from a data frame?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do you slice a DataFrame by index?

Slicing Rows and Columns by Index Position When slicing by index position in Pandas, the start index is included in the output, but the stop index is one step beyond the row you want to select. So the slice return row 0 and row 1, but does not return row 2. The second slice [:] indicates that all columns are required.

What is Orient in JSON?

If 'orient' is 'records' write out line delimited json format. Will throw ValueError if incorrect 'orient' since others are not list like. bool.

How do I export Pandas DataFrame to JSON?

To convert the object to a JSON string, then use the Pandas DataFrame. to_json() function. Pandas to_json() is an inbuilt DataFrame function that converts the object to a JSON string. To export pandas DataFrame to a JSON file, then use the to_json() function.


2 Answers

  • 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]]}'
like image 103
piRSquared Avatar answered Oct 08 '22 19:10

piRSquared


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

like image 30
MichaelChirico Avatar answered Oct 08 '22 19:10

MichaelChirico