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}}
>>> 
Alex
  • 3,946
  • 11
  • 38
  • 66

2 Answers2

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}]
titipata
  • 5,321
  • 3
  • 35
  • 59
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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252