3

The pandas to_json function for some reason is converting the index of the dataframe into strings. Is there a way to avoid this?

>>> import pandas as pd
>>> df = pd.DataFrame({"a" : [1,2,3], "b" : [2,3,4]})
>>> df.to_json(orient = "index")
'{"0":{"a":1,"b":2},"1":{"a":2,"b":3},"2":{"a":3,"b":4}}'
>>> import json
>>> json.loads(df.to_json(orient="index"))
{'2': {'a': 3, 'b': 4}, '1': {'a': 2, 'b': 3}, '0': {'a': 1, 'b': 2}}
>>> 
0

2 Answers 2

2

One way is to use orient='records' instead.

df.to_json(orient='records')

Then

json.loads(df.to_json(orient="records"))

will give output as

[{'a': 1, 'b': 2}, {'a': 2, 'b': 3}, {'a': 3, 'b': 4}]
Sign up to request clarification or add additional context in comments.

Comments

2

Keys in json cannot be int. Better it is explained here.

There is one possible solution - use parameter split in to_json if need store index values as int:

df = pd.DataFrame({"a" : [1,2,3], "b" : [2,3,4]}, index=[100,200,300])
print (df)
     a  b
100  1  2
200  2  3
300  3  4

a = df.to_json(orient = "split")
print (a)
{"columns":["a","b"],"index":[100,200,300],"data":[[1,2],[2,3],[3,4]]}

print (pd.read_json(a, orient='split'))
     a  b
100  1  2
200  2  3
300  3  4

1 Comment

This gives me a ValueError: Length of values (10) does not match length of index (2)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.