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?
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.
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.
If 'orient' is 'records' write out line delimited json format. Will throw ValueError if incorrect 'orient' since others are not list like. bool.
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.
json
with to_json(orient='split')
json
module to load that string to a dictionaryindex
key with del json_dict['index']
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]]}'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With