Skip to main content
 首页 » 编程设计

python之如何使用 iterrows 通过函数循环数据帧,该函数采用 3 个参数来填充新数据帧

2024年11月24日8落叶无声

我有什么:

1) GPS 坐标列表:纬度、经度和 ID。

2) 一个定义的函数来抓取最近 24 小时的每小时温度和湿度数据。它返回一个包含 3 列的数据框:温度、湿度、ID 和每小时数据作为 DatetimeIndex。该函数接受 3 个参数:lat、lon、ID。

我想要的:

  • 编辑函数以在每次传递 iterrows 时加入 ID 列

这是适用于一个纬度/经度/ID 集的函数:

# grab only weather of interest 
attributes = [u'temperature', u'humidity'] 
 
# 24 hours ago #round to closest hour 
date = dt.datetime.now().replace(microsecond=0,second=0,minute=0) -  
dt.timedelta(hours=24) 
 
#initalize 
times = [] 
data = {} 
for attr in attributes: 
    data[attr] = [] 
 
def scrape_weather(LAT, LON, Id): 
    for offset in range(1,2): #i.e 1 day 
        forecast = forecastio.load_forecast(api_key, LAT, LON,  
    time=date+dt.timedelta(offset), units = 'ca' ) 
        h = forecast.hourly() 
        d = h.data 
        for p in d: 
            times.append(p.time) 
            try: 
                for i in attributes: 
                    data[i].append(p.d[i]) 
            except: 
                print(KeyError) 
 
    df2 = pd.DataFrame(data) 
    df1 = pd.DataFrame(times) 
 
    df1.reset_index(drop=True, inplace=True) 
    df2.reset_index(drop=True, inplace=True) 
    dfweather = pd.concat([df1, df2], axis=1) 
 
    dfweather['ID'] = Id 
    dfweather = dfweather.set_index(pd.DatetimeIndex(dfweather[0])) 
    dfweather = dfweather.drop([0], axis=1) 
 
    return dfweather 

当使用 lat/lon/Ids 传递数据框的单列时,这很好用

scrape_weather(df.at[0,'latitude'],df.at[0,'longitude'], df.at[0,'Id']) 

但是当我经过的时候

for index, row in dummy_gps.iterrows(): 
    test = scrape_weather(row['longitude'],row['latitude'], row['Id']) 

预期的结果看起来像这样:

                 temperature humidity ID 
 
2019-05-14 07:00:00 22.58   0.34    1 
2019-05-14 08:00:00 20.50   0.42    1 
....  
2019-05-14 07:00:00 22.58   0.34    2 
2019-05-14 08:00:00 20.50   0.42    2 
.... 

但是 ID 是错误的,只有一个 ID 被复制粘贴到每个人身上,如下所示:

                 temperature humidity ID 
 
2019-05-14 07:00:00 22.58   0.34    2 
2019-05-14 08:00:00 20.50   0.42    2 
....  
2019-05-14 07:00:00 22.58   0.34    2 
2019-05-14 08:00:00 20.50   0.42    2 
.... 

所以我不确定在天气抓取功能的哪个位置添加 ID 逻辑以确保每个 ID 都与每个预报相关联

请您参考如下方法:

新答案

import pandas as pd 
import forecastio 
import datetime as dt 
 
 
def scrape_weather(row): 
    forecast = forecastio.load_forecast(api_key, 
                                        lat = row['latitude'],  
                                        lng = row['longitude'],  
                                        time = date, 
                                        units = 'ca' ) 
    h = forecast.hourly() 
    d = h.data 
    dfweather = pd.DataFrame({'times': [p.time for p in d], 
                              'temps': [p.temperature for p in d], 
                              'humidity': [p.humidity for p in d], 
                              'gatewayID': row['Id'] 
                             }) 
 
    return dfweather 
 
 
# Sample dataframe 
id_col = [1, 2, 3, 4, 5, 6, 7] 
lng = ['86.44511', '-121.13295', '-162.74005', '22.34765', '-152.18709', '-152.18709', '-107.65340'] 
lat = ['-18.67825', '-20.84215', '57.31227', '6.15070', '-27.72616', '-27.72616', '6.15863'] 
df = pd.DataFrame({'Id':id_col, 'latitude':lat, 'longitude':lng}) 
 
api_key = ############################### 
 
# 24 hours ago #round to closest hour 
date = dt.datetime.now().replace(microsecond=0,second=0,minute=0) - dt.timedelta(hours=24) 
 
out = df.apply(scrape_weather, axis=1) 
out = pd.concat([df for df in out]) 

旧答案

如果我没理解错的话,你能做这样的事情吗?

df = pd.DataFrame({'LAT':[1,2,3],'LON':[1,2,3],'ID':[1,2,3]}) 
 
def scrape_weather(row): 
    temperature = row['LAT'] # change this to what you need to do 
    humidity = row['LON'] # change this to what you need to do 
    id = row['ID'] # change this to what you need to do 
    return temperature, humidity, id 
 
new_df = pd.DataFrame(columns=['temp', 'hum', 'id']) 
new_df['temp'], new_df['hum'], new_df['id'] = df.apply(scrape_weather, axis=1) 

这给了我

    temp    hum     id 
0   1       2       3 
1   1       2       3 
2   1       2       3