I'm pretty new to pandas/python and coding overall. Thus I got a question about coding sums of columns with pandas.
I have a 306x7 dataframe about past soccer results. Now I want to sum both the home goals and away goals for each club and put it into a new dataframe (18 rows for 18 clubs and 2 columns for homegoals and awaygoals fullseason).
Could anyone give me an idea on how to proceed?
teams = Liga2['HomeTeam'].unique()
df = pd.DataFrame(index=teams, columns=['FTHG','FTAG'])
for team in teams:
df.loc[team, 'FTHG'] = [Liga2.HomeTeam == team].FTHG.sum()
df.loc[team, 'FTAG'] = [Liga2.AwayTeam == team].FTHG.sum()
Error:
AttributeError Traceback (most recent call last)
<ipython-input-12-a1b735dbadf3> in <module>
4
5 for team in teams:
----> 6 df.loc[team, 'FTHG'] = [Liga2.HomeTeam == team].FTHG.sum()
7 df.loc[team, 'FTAG'] = [Liga2.AwayTeam == team].FTHG.sum()
AttributeError: 'list' object has no attribute 'FTHG'
This is the df:
https://i.stack.imgur.com/x7pLv.jpg
Thank you for your ideas.