检索熊猫集中的数据

时间:2019-02-28 19:38:13

标签: json pandas api

我正在从Open Weather Map API检索数据。我有以下代码,可以从500多个城市中提取当前的天气,我想要的日志可以将我的数据分成50组

我做了一个我想改进的无效方法!

非常感谢!

x = 1

for index, row in df.iterrows():

base_url = "http://api.openweathermap.org/data/2.5/weather?"

units = "imperial"
query_url = f"{base_url}appid={api_key}&units={units}&q="
city = row['Name'] #this comes from a df

response = requests.get(query_url + city).json()


try:        
df.loc[index,"Max Temp"] = response["main"]["temp_max"]

if index < 50: 
print(f"Processing Record {index} of Set {x} | {city}")

elif index <100: 

x = 2

print(f"Processing Record {index} of Set {x} | {city}")

elif index <150: 

x = 3

print(f"Processing Record {index} of Set {x} | {city}")

except (KeyError, IndexError):
    pass
    print("City not found. Skipping...")

0 个答案:

没有答案