Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 面临将字典中的数据添加到datafram的问题_Python_Pandas_Dataframe_Openweathermap - Fatal编程技术网

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

Python 面临将字典中的数据添加到datafram的问题,python,pandas,dataframe,openweathermap,Python,Pandas,Dataframe,Openweathermap,在创建将响应存储在列中的函数时遇到问题, 例如传递城市名称,作为响应,获取最高温度、最低温度和压力的详细信息。我想将其存储在新列中 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): o

在创建将响应存储在列中的函数时遇到问题, 例如传递城市名称,作为响应,获取最高温度、最低温度和压力的详细信息。我想将其存储在新列中

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)
要根据城市名称将每个天气详细信息添加为列吗

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

数据帧类似于:


您可以从功能中返回词典

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'])

感谢您的重播,但问题是如何创建一个函数来获得所有细节,不想创建像上面那样的单独函数。(问题更新)df[‘风速’、‘最高温度’、‘压力’、‘最低温度’]=df[‘城市’]。应用(λx:获取天气(x))。您想要这样的东西吗?在这种情况下,每个列函数都将调用,因此需要花费大量时间。在单次调用中,获取所有详细信息,因此希望使用我可以在df中存储整行数据的方式。没有必要创建函数,我们可以只使用查找来获取数据并在df中存储数据。在这种情况下,调用使用city的函数来获取响应的dict,使用单行创建数据帧,将dict中的值添加到行中,并将其附加到父数据帧。我相信没有一行代码用于此目的。