-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_processor.py
80 lines (57 loc) · 2.65 KB
/
data_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pandas as pd
from datetime import datetime, timedelta
def get_five_games(data, teamA, teamB=None):
if teamB is None:
filtered_games = data[
(
((data["home_team"] == teamA) | (data["away_team"] == teamA))
& (data["tournament"] != "UEFA Euro qualification")
)
]
else:
filtered_games = data[
(
((data["home_team"] == teamA) & (data["away_team"] == teamB))
| ((data["home_team"] == teamB) & (data["away_team"] == teamA))
)
& (data["tournament"] != "UEFA Euro qualification")
]
# Explicitly modify data row to datetime
filtered_games.loc[:, "date"] = pd.to_datetime(filtered_games["date"])
filtered_games = filtered_games.sort_values(by="date", ascending=False)
recent_10_games = filtered_games.head(10)
current_date = datetime.now()
twenty_years_ago = current_date - timedelta(days=365 * 20)
# Filter out games without scores
games_with_scores = recent_10_games.dropna(subset=["home_score", "away_score"])
recent_games_with_scores = games_with_scores[
games_with_scores["date"] > twenty_years_ago
].head(5)
recent_games_with_scores.loc[:, "date"] = pd.to_datetime(filtered_games["date"]).dt.date
return recent_games_with_scores.loc[
:, ["date", "home_team", "away_team", "home_score", "away_score", "tournament"]
].to_string(index=False)
def get_qualification_games(data, team):
filtered_games = data[
((data["home_team"] == team) | (data["away_team"] == team))
& (data["tournament"] == "UEFA Euro qualification")
]
# Explicitly modify data row to datetime
filtered_games.loc[:, "date"] = pd.to_datetime(filtered_games["date"])
filtered_games = filtered_games.sort_values(by="date", ascending=False)
two_years_ago = datetime.now() - timedelta(days=2 * 365)
filtered_games = filtered_games[filtered_games["date"] > two_years_ago]
filtered_games.loc[:, "date"] = pd.to_datetime(filtered_games["date"]).dt.date
return filtered_games.loc[
:, ["date", "home_team", "away_team", "home_score", "away_score", "tournament"]
].to_string(index=False)
def get_top_goal_scorers(goals_data, team):
goals_data.loc[:, "date"] = pd.to_datetime(goals_data["date"])
two_years_ago = datetime.now() - timedelta(days=2 * 365)
team_goals = goals_data[
(goals_data["team"] == team) & (goals_data["date"] > two_years_ago)
]
# Filter out own goals
team_goals = team_goals[~team_goals["own_goal"]]
top_scorers = team_goals["scorer"].value_counts().head(5)
return top_scorers