将 iteritems-Result 存储在数据框中
Store iteritems-Result in dataframe
目标:
在我的 nfl teams dataframe (df_teams) 中添加一列 ('Team_url') 以及每个球队的网站-url.
问题:
如果我打印 url,它工作得很好。如果我尝试将其存储到 df_teams['Team_url'],它只会存储 iteritems 的最后一个结果。
数据:
df_teams['Team_web']
0 Arizona-Cardinals
1 Chicago-Bears
2 Green-Bay-Packers
3 New-York-Giants
4 Detroit-Lions
.
.
.
31 Houston-Texans
代码:
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(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
问题:
如何在数据框列中存储为 url_1 打印的内容?
df['Team_url'] = 'https://www.nfl.com/teams/' + df['Team_web'] + '/roster'
目标: 在我的 nfl teams dataframe (df_teams) 中添加一列 ('Team_url') 以及每个球队的网站-url.
问题: 如果我打印 url,它工作得很好。如果我尝试将其存储到 df_teams['Team_url'],它只会存储 iteritems 的最后一个结果。
数据:
df_teams['Team_web']
0 Arizona-Cardinals
1 Chicago-Bears
2 Green-Bay-Packers
3 New-York-Giants
4 Detroit-Lions
.
.
.
31 Houston-Texans
代码:
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(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
问题: 如何在数据框列中存储为 url_1 打印的内容?
df['Team_url'] = 'https://www.nfl.com/teams/' + df['Team_web'] + '/roster'