在迭代列以进行API请求时出现类型错误。

huangapple go评论122阅读模式
英文:

Getting type error while iterating through columns to make API request

问题

我有一个来自Kaggle的数据集(https://www.kaggle.com/datasets/michaelbryantds/tornadoes/),在这个数据集中,我正在迭代四列(日期、纬度、经度、时间)以从这个API(https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/)进行API请求。我已经抽取了250个龙卷风的加权样本(以便不超过每天1000个请求的限制)。

目标是使用这三个数据点将温度、湿度和气压数据附加到每个龙卷风。

我遇到的问题是,当我尝试创建一个简单的“for”循环来循环遍历并打印响应时,我收到以下错误消息:

TypeError: 'float' object is not subscriptable

在我尝试编写将其附加到DF的部分之前。有人可以指点我正确的方向吗?

df = Sample_df

Sample_df.slat.astype('float')
Sample_df.slon.astype('float')

for (tornado, lon) in df.iterrows():
    date = lon['date']
    lat = lon['slat']
    lon = lon['slon']
    t = lon['time']
    
    apikey = 'MY_API_KEY'
    url = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{},{}/{}T{}?key={}".format(lat, lon, date, t, apikey)
    response = requests.get(url)  
    response_dict = response.json()
    print(response_dict)

希望这可以帮助你解决问题。

英文:

I have a dataset from Kaggle (https://www.kaggle.com/datasets/michaelbryantds/tornadoes/) where I am iterating through four columns (date, slat, slon, time) to make an API request from this API https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/. I have drawn a weighted sample of 250 tornadoes (so I won't surpass the 1000 request/day limit).

The goal is to use those three datapoints to append the temperature, humidity, and pressure data to each tornado.

The problem I am having is I am getting

> TypeError: 'float' object is not subscriptable

when I try to make a simple "for" loop to cycle through and print the responses before I even try to code in the portion to append it to the DF. Can someone point me in the right direction here?

df = Sample_df

Sample_df.slat.astype('float')
Sample_df.slon.astype('float')

for (tornado,lon) in df.iterrows():
    date = lon['date']
    lat = lon['slat']
    lon = lon['slon']
    t = lon['time']
    
    apikey = 'MY_API_KEY'
    url = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{},{}/{}T{}?key={}".format(lat,lon,date,time,apikey)
    response = requests.get(url)  
    response_dict = response.json()
    print(response_dict)

答案1

得分: 1

lon在循环中被重新定义为浮点数lon = lon['slon']。请选择另一个名称用于此变量。

此外,time未定义,但 t已定义。

英文:

You have redefined lon in cycle to a float lon = lon['slon'].
Choose another name for this variable.

Also time is not defined, but t is.

答案2

得分: 0

您的问题是在提取所有数据之前重新分配了变量 lon

for (tornado,lon) in df.iterrows():
    date = lon['date']
    lat = lon['slat']
    lon = lon['slon']  # 用浮点值覆盖了变量 lon
    t = lon['time']  # 尝试访问 lon 的旧值

您可以通过为变量指定不同的名称来解决此问题:

for (tornado,data) in df.iterrows():
    date = data['date']
    lat = data['slat']
    lon = data['slon']
    t = data['time']
英文:

Your issue that you've used reassigned the variable lon before you finished extracting all the data:

for (tornado,lon) in df.iterrows():
    date = lon['date']
    lat = lon['slat']
    lon = lon['slon']  # Overwrites the variable lon with a float value
    t = lon['time']  # Attempts to access the old value of lon

You can fix this by giving the variable a different name:

for (tornado,data) in df.iterrows():
    date = data['date']
    lat = data['slat']
    lon = data['slon']
    t = data['time']

huangapple
  • 本文由 发表于 2023年8月11日 03:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定