Python Forum
Add column headers to dataframe
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add column headers to dataframe
#4
OK, here is a simple example how to use apply in your code

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.
Reply


Messages In This Thread
Add column headers to dataframe - by chisox721 - May-14-2018, 06:13 PM
RE: Add column headers to dataframe - by volcano63 - May-15-2018, 09:59 AM
RE: Add column headers to dataframe - by chisox721 - May-15-2018, 04:49 PM
RE: Add column headers to dataframe - by volcano63 - May-16-2018, 06:01 AM
RE: Add column headers to dataframe - by chisox721 - May-17-2018, 05:36 PM
RE: Add column headers to dataframe - by volcano63 - May-20-2018, 12:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  concat 3 columns of dataframe to one column flash77 2 980 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 1,202 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,585 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  New Dataframe Column Based on Several Conditions nb1214 1 1,870 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Putting column name to dataframe, can't work. jonah88888 1 1,893 Sep-28-2021, 07:45 PM
Last Post: deanhystad
  Setting the x-axis to a specific column in a dataframe devansing 0 2,110 May-23-2021, 12:11 AM
Last Post: devansing
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,383 May-17-2021, 07:02 AM
Last Post: lorensa74
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 3,078 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,370 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Pandas DataFrame and unmatched column sritsv19 0 3,115 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020