Pandas 使用 iterrows 根据数据框列中的值创建包含团队名称的 csv 文件

Pandas create csv file with team name based on value in data frame column using iterrows

目标: 为每个 NFL 球队名单创建一个数据框

基础数据框:

print(df_teams)
                        Team  Active since  Regular season record  ...  Games Played                  Team_web                                           Team_url
0          Arizona Cardinals          1920                    553  ...          1362         Arizona-Cardinals  https://www.nfl.com/teams/Arizona-Cardinals/ro...
1              Chicago Bears          1920                    761  ...          1422             Chicago-Bears     https://www.nfl.com/teams/Chicago-Bears/roster
2          Green Bay Packers          1921                    743  ...          1408         Green-Bay-Packers  https://www.nfl.com/teams/Green-Bay-Packers/ro...
3            New York Giants          1925                    692  ...          1370           New-York-Giants   https://www.nfl.com/teams/New-York-Giants/roster
4              Detroit Lions          1930                    559  ...          1269             Detroit-Lions     https://www.nfl.com/teams/Detroit-Lions/roster

代码:

# scrape roster of teams
for index, row in df_teams.iterrows():
    url = (row['Team_url'])
    # Extract tables
    dfs = pd.read_html(url)
    # Get first table
    df_roster = dfs[0]
    # Check
    print(df_roster)
    # Write to csv
    df_roster.to_csv('roster' + df_teams['Team_web'] + '.csv', sep=',', index=False)

返回错误:

TypeError: argument of type 'method' is not iterable

打印(df_roster) 工作正常。

问题: 如何在遍历数据框时使用 df_teams('Team_web') 中的值添加到 csv 文件名?

df_roster.to_csv('roster' + df_teams['Team_web'] + '.csv', sep=',', index=False)

我自己发现错误:

# Write to csv
df_roster.to_csv('roster' + (row['Team_web']) + '.csv', sep=',', index=False)

成功了