Python Forum

Full Version: Python pandas dataframe simulate football points system.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello:
import pandas as pd
import numpy as np

dfGames = pd.DataFrame.from_items([('TeamsHome', ['A', 'B', 'C']), ('TeamsAway', ['B', 'C', 'A']),('Games', [1, 1, 1]), ('HomeGoals', [2, 0, 1]), ('AwayGoals', [0, 0, 2])])
print(dfGames)

dfPoints = pd.DataFrame.from_items([('Teams', ['A', 'B', 'C']),  ('Games', [1, 1, 1]), ('Points', [6, 1, 1])])
print(dfPoints)
Let’s say, we have a small football tournament of 3 teams: A, B and C.
For the first round: A beat B with score, 2:0; B and C had a draw, 0:0; C lost to A, 1:2. The teams and goals are shown in dfGames.
Now, I want to create a dataframe to show each team’s points. The rule is: if the goals are more than the opponent’s goals, then it is a win, which gives the winner 3 points, the loser gets 0 point; if the goals are the same as the opponent’s goals, then it is a draw, each team gets 1 point.
I want to write a function to return a dataframe as dfPoints, I need columns: Teams, Games and Points.
In this example, as team A has 2 wins, therefore, team A has 6 points, team B has one lose, one draw, so it has 1 point, team C is the same as team B.
But I can’t figure this out how to do this using the pandas functions.