mardi 26 novembre 2019

Issue while writing complex if-else condition along in Python

I am having and API from which I retrieve data store into a dataframe using requests.get. The API gives two types response status code =200 and !=200. If the respnse.staus_code=200then I need populate the dataframe with values from response.text shown below else respnose.staus_code!=200then populate with "". I am not completly write the if -elif condition properly and attaching my imcomplete code below. Could you help

response.text

'{"results":[{"place":{"type":"coord","value":"44.164:28.641","lat":44.164,"lon":28.641,"tz":"Europe/Bucharest"},"measures":[{"ts":1575331200000,"date":"2019-12-03","temperature_2m":11.78,"temperature_2m_min":11.75,"temperature_2m_max":12.46,"windspeed":3.25,"direction":"SSW","wind_gust":5.43,"relative_humidity_2m":88,"sea_level_pressure":1014,"sky_cover":"cloudy","precipitation":0.0,"snow_depth":0,"thunderstorm":"N","fog":"M"}]},{"place":{"type":"coord","value":"53.546:9.98","lat":53.546,"lon":9.98,"tz":"Europe/Berlin"},"measures":[{"ts":1575331200000,"date":"2019-12-03","temperature_2m":-0.55,"temperature_2m_min":-0.8,"temperature_2m_max":-0.35,"windspeed":3.65,"direction":"WSW","wind_gust":8.62,"relative_humidity_2m":88,"sea_level_pressure":1025,"sky_cover":"mostly_clear","precipitation":0.0,"snow_depth":0,"thunderstorm":"N","fog":"M"}]}]}

The code to store into the dataframe

d = json.loads(response.text)

out = []
for x in d['results']:
    t = x['place']['type']
    v = x['place']['value']
    for y in x['measures']:
        y[t] = v
        out.append(y)

df = pd.DataFrame(out)

My code with if -elif condition( I need help in completing this one)

   df_results = pd.DataFrame( list(zip(Unique_Coords)), columns =['Unique_Coords'])
    today_date =date.today().strftime("%Y-%m-%d")
    shifted_date = date.today() + timedelta(days=7)
    shifted_date =shifted_date.strftime("%Y-%m-%d")
    results ={}
    df_results["date"] = ""
    df_results["direction"] = ""
    df_results["fog"] = ""
    df_results["precipitation"] = ""
    df_results["relative_humidity_2m"] = ""
    df_results["sea_level_pressure"] = ""
    df_results["snow_depth"] = ""
    df_results["temperature_2m"] = ""
    df_results["temperature_2m_max] = ""
    df_results["temperature_2m_min"] = ""
    df_results["thunderstorm"] = ""
    df_results["ts"] = ""
    df_results["wind_gust"] = ""
    df_results["windspeed"] = ""

    headers = { 'Authorization': 'Api-Key',}

    for i, row in df_results.iterrows():
        d_ = (('coords', str(row["Unique_Coords"])), ('fromDate', today_date), ('toDate', shifted_date))
        response = requests.get('https://example.com/weather/v2/forecasts', headers=headers, params=d_)
        if (response.status_code)!=200:
           df_results["date"].iloc[i] = ""
           df_results["direction"].iloc[i] = ""
           df_results["fog"].iloc[i] = ""
           df_results["precipitation"].iloc[i] = ""
           df_results["relative_humidity_2m"].iloc[i] = ""
           df_results["sea_level_pressure"].iloc[i] = ""
           df_results["snow_depth"].iloc[i] = ""
           df_results["temperature_2m'"].iloc[i] = ""
           df_results["temperature_2m_max"].iloc[i] = ""
           df_results["temperature_2m_min"].iloc[i] = ""
           df_results["thunderstorm"].iloc[i] = ""
           df_results["ts"].iloc[i] = ""
           df_results["wind_gust"].iloc[i] = ""
           df_results["windspeed"].iloc[i] = ""
        elif (response.status_code)==200:

How to write the condition for status_code 200?

Aucun commentaire:

Enregistrer un commentaire