Search code examples
python-3.xpandasdataframeiteritems

Store iteritems-Result in dataframe


Goal: Add a column ('Team_url') to my nfl teams dataframe (df_teams) with each teams website-url.

Problem: If I print the url, it works just fine. If I try to store it to df_teams['Team_url'], it only stores the last result of the iteritems.

Data:

df_teams['Team_web']

0            Arizona-Cardinals
1                Chicago-Bears
2            Green-Bay-Packers
3              New-York-Giants
4                Detroit-Lions
.
.
.
31              Houston-Texans

Code:

for i, j in df_teams['Team_web'].iteritems():
    url_1 = "https://www.nfl.com/teams/{0}/roster".format(j)
    df_teams['Team_url'] = url_1

Print:

print(url_1):

https://www.nfl. com/teams/Arizona-Cardinals/roster
https://www.nfl. com/teams/Chicago-Bears/roster
.
.
.
https://www.nfl.com/teams/Houston-Texans/roster

print(df_teams['Team_url'])

0     https://www.nfl.com/teams/Houston-Texans/roster
1     https://www.nfl.com/teams/Houston-Texans/roster
2     https://www.nfl.com/teams/Houston-Texans/roster

Questions: How can I store what is printed for the url_1 in the dataframe column?


Solution

  • df['Team_url'] = 'https://www.nfl.com/teams/' + df['Team_web'] + '/roster'
    

    enter image description here