简体   繁体   中英

Convert json data into dataframe

I am unable to flatten the json data from this API into a dataframe. I have tried using json_normalize but I gives a NotImplemented error. Can someone help me with it? I need the columns: stationId, start, timestep, temperature where there are several values for temperature and rest of the columns should have same values.

import requests
import json
import pandas as pd

response_API = requests.get('https://dwd.api.proxy.bund.dev/v30/stationOverviewExtended?stationIds=10865,G005')
print(response_API.status_code)
data = response_API.text
json.loads(data)

df= ?

You can do it many ways, but your current approach should use json() instead of text

import requests
import json
import pandas as pd

response_API = requests.get('https://dwd.api.proxy.bund.dev/v30/stationOverviewExtended?stationIds=10865,G005')
print(response_API.status_code)
data = response_API.json()        <--- it should be json()
print(data)

OR directly read json to df from the URL using read_json()

df = pd.read_json("https://dwd.api.proxy.bund.dev/v30/stationOverviewExtended?stationIds=10865,G005")
print(df)

Edit:

import requests
import json
import pandas as pd

response_API = requests.get('https://dwd.api.proxy.bund.dev/v30/stationOverviewExtended?stationIds=10865,G005')
print(response_API.status_code)
data = response_API.json()

result = []
for station, value in data.items():
    for forecast, val in value.items():
        if forecast in ['forecast1', 'forecast2']:
            result.append(val)
df = pd.DataFrame(result)
print(df)

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