简体   繁体   中英

nested json api respone to python dataframe

i am using api response to get indian pin codes list along with state , country and many names, which are in postofflice field, i want dedicated dataframe for that postoffice field, please help

import pandas as pd
import numpy as np
import json
import requests
from pandas.io.json import json_normalize

response = requests.get("https://api.postalpincode.in/pincode/110094")
data = json.loads(response.text)

json_normalize(data)

res = [ sub['PostOffice'] for sub in data ]

You could do something like this:

import pandas as pd
import json
import requests

response = requests.get("https://api.postalpincode.in/pincode/110094")
data = json.loads(response.text)

postOffices = pd.DataFrame(data[0]['PostOffice'])

After that you can access every element like this:

print(postOffices.loc[0])

Output:

Name                        Dayalpur
Description                     None
BranchType        Branch Post Office
DeliveryStatus          Non-Delivery
Circle                         Delhi
District            North East Delhi
Division                  Delhi East
Region                         Delhi
Block                             NA
State                          Delhi
Country                        India
Pincode                       110094
Name: 0, dtype: object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM