0

I have API output in the below format.

reponse.text

'{"position": {"lat": 1.352083, "lon": 103.819836}, "mapView": {"N": 1.4784001, "E": 104.0945001, "S": 1.1496, "W": 103.594}}\n'

I want to convert this output to pandas dataframe .

My code

import json
d = json.loads(response.text)
df = pd.DataFrame(d)

Current Output

    position    mapView
E   NaN            104.0945
N   NaN             1.4784
S   NaN             1.1496
W   NaN            103.5940
lat 1.352083        NaN
lon 103.819836      NaN

My Expected Output

lat       lon
1.352083  103.819836 

How can this be achieved in python?

3 Answers 3

2

You are very close. import json_normalize from pandas and then you can simply use

from pandas.io.json import json_normalize

jsonData = json.loads(response.text)

df = json_normalize(jsonData)

print(df)

position.lat  position.lon  mapView.N  mapView.E  mapView.S  mapView.W
    1.352083    103.819836     1.4784   104.0945     1.1496    103.594

then simply delete extra columns and rename it properly.

Sign up to request clarification or add additional context in comments.

Comments

0
import pandas as pd
import json
d = json.loads(response.text)

df =pd.DataFrame([d['position']])

enter image description here

Comments

0

you are passing as argument the whole dictionary created by the json parser, while you only want to use the position key.

please go check the DataFrame constructor documentation page here for additional parameters.

>>> import pandas as pd
>>> import json
>>> api_resp = json.loads('{"position": {"lat": 1.352083, "lon": 103.819836}, "mapView": {"N": 1.4784001, "E": 104.0945001, "S": 1.1496, "W": 103.594}}\n')
>>> api_resp
{'position': {'lat': 1.352083, 'lon': 103.819836}, 'mapView': {'N': 1.4784001, 'E': 104.0945001, 'S': 1.1496, 'W': 103.594}}
>>> df = pd.DataFrame(data=api_resp['position'], index=[0])
>>> df
        lat         lon
0  1.352083  103.819836

Comments

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.