OK, here is a simple example how to use apply in your code
PS pandas is an amazing tool which I just started learning myself - but you have to understand, DataFrame and Series are objects intended for processing as a whole - otherwise, you abuse pandas. Learn it properly
url_template = "http://api.wunderground.com/api/88fb15b49e765a43/conditions/q/{state}/{city}.json" def get_weather(row, *fileds): url = requests.get(url_template.format(state=row['State'],city=row['City'])) observation = url.json()['current_observation'] return [observation[field] for field in fields] fields = ['temp_f', 'relative_humidity'] stadiums = pd.DataFrame([['SFG', 'CA', 'San Francisco'], ['NYY', 'NY', 'Bronx']], columns=['Team', 'State', 'City']) print(stadiums.apply(lambda r: get_weather(r, *fields), axis=1).values)And the result is
Output:[list([54.7, '82%']) list([63.0, '83%'])]
now, stadium[fields] = pd.DataFrame(<result>)will update your stadiums DataFrame with new fields - but you have to convert the result to a form accepted by pandas.DataFrame constructor. I will let you experiment with that on your own.
PS pandas is an amazing tool which I just started learning myself - but you have to understand, DataFrame and Series are objects intended for processing as a whole - otherwise, you abuse pandas. Learn it properly
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.