Home > Blockchain >  Convert json data into dataframe
Convert json data into dataframe

Time:09-12

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= ?

CodePudding user response:

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)
  • Related