面临将字典中的数据添加到数据框中的问题

facing issue to add Data from dictionary into datafram

在创建将响应存储在列中的函数时遇到问题, 就像传递城市名称和作为响应获取 max_temp、min_temp、压力的详细信息一样。我想将其存储在新列中。

import pyowm
from pyowm.utils import config
from pyowm.utils import timestamps

api_key = {key from openweather(free)}
mgr = owm.weather_manager()
data =[]
def get_weather(city):
    observation = mgr.weather_at_place(city)
    l = observation.weather
    Wind_Speed = l.wind()['speed']
    Temp = l.temperature('celsius')['temp']
    Max_temp = l.temperature('celsius')['temp_max']
    Min_temp = l.temperature('celsius')['temp_min']
    #Heat_index = l.heat_index
    Humidity = l.humidity
    Pressure = l.pressure['press']
    weather = {"City": city,"Wind_Speed" : Wind_Speed, "Temp": 
    Temp,"Max_temp":Max_temp, "Min_temp":Min_temp, "Humidity":Humidity,
                  "Pressure":Pressure}
    
    return weather

for city in df2['City']:
    get_weather(city)
    df = df.append(data, True)

想要将每个天气详细信息添加为基于城市名称的列

想要创建一个将所有详细信息存储在列中的函数, 不想创建单独的函数。

数据框如下:

您可以 return 您的函数中的字典。

def get_weather(city):
    observation = mgr.weather_at_place(city)
    l = observation.weather
    Wind_Speed = l.wind()['speed']
    Temp = l.temperature('celsius')['temp']
    Max_temp = l.temperature('celsius')['temp_max']
    resp=dict()
    resp['Wind_Speed ']=Wind_Speed 
    resp['Temp']=Temp 
    resp['Max_temp']=Max_temp  
    return resp

df["Wind_speed"] = df["city"].apply(lambda x: get_weather(x)['Wind_Speed'])
df["Temp"] = df["city"].apply(lambda x: get_weather(x)['Temp'])
df["Max_temp"] = df["city"].apply(lambda x: get_weather(x)['Max_temp'])