python-3.xpandasdataframeiteritems

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


Goal: Create a data frame per NFL team roster

Basis Data frame:

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

Code:

# 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)

Returned error:

TypeError: argument of type 'method' is not iterable

the print(df_roster) works fine.

Question: How can I use the value in df_teams('Team_web') to add to the csv-file name while iterating through my data frame?

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

Solution

  • I found the error myself:

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

    does the trick