I’m just learning Python using the “use others’ code, modify it for myself, resolve problems as they arise” method, although I did take on online introductory class with MIT. And I know other programming languages such as SQL, VBA, etc, so I understand that logic and syntax are important, but don’t know enough about those yet for Python, so please be gentle!
I’m having a problem understanding the following lines of code. What it’s intending to do is to build a “table” of averages from the previous 5 games of a list of NFL football teams. I’m getting the error message “TypeError: Could not convert [‘CIN’] to numeric” at the last line of the code (the “append” line). I KNOW that the line is calculating averages, so on the surface the error makes sense (CIN is a string; it’s the abbreviated team designator for CINcinnatti), but I don’t know how to resolve the error.
What I want as an outcome is Team, PassAtt, PassComp, PassYds, etc as column names, with items like CIN, 25, 22, 57 etc as values below those headers.
I can provide as much or as little code as necessary to help me get through this step, so let me know if you need more than the following.
Following is the code
# At each row of this dataset, get the team name, find the stats for that team during the last 5 games, and average these stats (avg_stats_per_team).
avg_stat_columns = ['Team', 'PPG','PAttPerGm','PCmpPerGm','PYdsPerGm', 'PTDPerGm', 'PIntPerGm', 'SksPerGm', 'SkYdsPerGm', 'RuAttPerGm', 'RuYdsPerGm', 'RuTDPerGm', 'PenPerGm', 'PenYdsPerGm', 'OppPYdsPerGm', 'OppPTDPerGm', 'OppPIntPerGm', 'OppSksPerGm', 'OppSkYdsPerGm', 'OppRuAttPerGm', 'OppRuYdsPerGm', 'OppRuTDPerGm', 'OppPenPerGm', 'OppPenYdsPerGm' ]
stats_list = []
for index, row in team_stats_per_match.iterrows():
team_stats_last_five_matches = team_stats_per_match.loc[(team_stats_per_match['Name']==row['Name']) & (team_stats_per_match['Date']<row['Date'])].sort_values(by=['Date'], ascending=False)
# A Pandas axis refers to the data in rows (axis = 1) or columns (axis = 0)
# In the line below, iloc[0:5,3:] means to take the first five rows (0 doesn't count) and all columns beginning with column 2 (the third column 0-1-2)
stats_list.append(team_stats_last_five_matches.iloc[0:5,:].mean(axis=0).values[3:23])
And here is an image of the actual error I’m getting (bottom) along with a screenshot of the source from which I’m trying to build the output.
I’ve tried different values for row/column start and end points, with varying results, but in each case I tried, this same error message occurred. I just can’t seem to get the append line to NOT try to average the team name, but I need the team name to be in the output table.
Thank you, in advance, for whatever insight you can provide.