2

I have a pandas dataframe with two columns

      Lat       Long
0   53.0294   53.3256
1   13.6363   63.3632
2   22.5353   55.2201
...

Now I need json file to be perfect for use it for Google Map API.

To look like this:

[{"Lat": "45.496275", "Long": "19.700225"}
{"Lat": "43.9332040422", "Long": "21.3821478025"}
{"Lat": "43.7236250523", "Long": "20.6935022429"}]

I have tried with this:

out = coord_frame.to_json(orient='records')

Ang get this:

[{\"Lat\":44.8242557024,\"Long\":20.4048512901},{\"Lat\":44.8242557024,\"Long\":20.4048512901}]

I don't need those slashes and I'm not sure if I can use this in javascript code of Google Map API.

3
  • Use df.to_dict('r')? Commented Sep 23, 2017 at 17:30
  • @Zero Thank you. :) Commented Sep 23, 2017 at 17:35
  • The problem is with how you are saving your json. Can you add that code as well? Commented Sep 23, 2017 at 18:01

1 Answer 1

1

You can use:

coord_frame.to_json('file.json', orient='records')

For writing dict to file use:

import json

out = coord_frame.to_dict(orient='records')

with open("file.json", 'w') as f:
    json.dump(out, f)
Sign up to request clarification or add additional context in comments.

2 Comments

But when I save it to txt, I got those slashes.
I try some test and 2 solution was added. Unfortunately I cannot simulate your problem with backslashes, but I hope it working nice.

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.