提问者:小点点

如何使用iterrows通过使用3个参数填充新数据帧的函数循环数据帧


我所拥有的:

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

2)一个定义的函数来刮取过去24小时的每小时的温度和湿度数据。它返回3列的数据帧:温度、湿度、ID和每小时数据作为日期时间索引。该函数接受3个参数:拉特、lon、ID。

我想要的是:

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

以下是适用于一个lat/lon/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与每个预测关联


共1个答案

匿名用户

新答案

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