Pandas dataframe to json without index

前端 未结 2 2034
北海茫月
北海茫月 2020-12-08 07:15

I\'m trying to take a dataframe and transform it into a partcular json format.

Here\'s my dataframe example:

DataFrame name: Stops
id    location
0           


        
相关标签:
2条回答
  • 2020-12-08 07:31

    If you want a Python dict (the JSON-object equivalent in python) not a JSON string:

    df.to_dict(orient='records')
    
    0 讨论(0)
  • 2020-12-08 07:39

    You can use orient='records'

    print df.reset_index().to_json(orient='records')
    
    [
         {"id":0,"location":"[50, 50]"},
         {"id":1,"location":"[60, 60]"},
         {"id":2,"location":"[70, 70]"},
         {"id":3,"location":"[80, 80]"}
    ]
    
    0 讨论(0)
提交回复
热议问题