python - 如何将函数的结果存储到具有相关列的数据框
问题描述
从函数返回数据作为字典并将其存储在数据框中。使用 for 循环运行它时出现错误。
import pyowm
from pyowm.utils import config
from pyowm.utils import timestamps
owm = pyowm.OWM(" your free api key from OpenWeatherMap")
mgr = owm.weather_manager()
data =[]
# Create function to get weather details
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 df_location['City']:
get_weather(city)
df = df.append(data, True)
想要将天气详细信息存储在与相关城市相同的 df 中。
解决方案
推荐阅读
- c# - 如何查看存储在 Couchbase lite 中的数据文档?
- siddhi - 无法在 Siddhi 中使用“正则表达式”http.status.code 处理响应
- wix - windows-installer 检查是否需要修复
- postgresql - 如何在 PostgreSql 函数的返回表中设置标识列?
- ruby-on-rails - Ruby lib console.so 加载错误和未定义符号
- python - 在函数中使用遗传算法(局部) - 最小化函数
- objective-c - 在Objective-C中使用精确的int字典值过滤字典的NSMutableArray
- c# - 如何在代码中设置 xaml DesignInstance 属性
- wordpress - 注意:未定义的属性:stdClass::$file_id Woocommerce
- api - 使用 Symfony 在 API 功能测试中创建模拟