Advertisement
samipote

Untitled

Apr 30th, 2024
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. def calculate_recent_performance(df, games=10):
  2.     """
  3.    Calculate the team's recent performance metrics for home and away teams efficiently, including current winning
  4.    and losing streaks.
  5.    :param df: DataFrame containing the game data.
  6.    :param games: Number of recent games to calculate metrics for.
  7.    :return: DataFrame with calculated metrics for both home and away teams.
  8.    """
  9.     # Prepare DataFrame for rolling calculations
  10.     # Calculate for home games
  11.     home_df = df.copy()
  12.     home_df['home_wins'] = home_df['home_win']
  13.     home_df['home_goals_scored'] = home_df['score_home']
  14.     home_df['home_goals_conceded'] = home_df['score_away']
  15.    
  16.     home_df.sort_values(['home_team', 'game_date'], inplace=True)
  17.  
  18.     # Rolling calculate win rate, goals scored, and goals conceded for home team
  19.     home_df['home_recent_win_rate'] = home_df.groupby('home_team')['home_wins'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  20.     home_df['home_avg_goals_scored'] = home_df.groupby('home_team')['home_goals_scored'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  21.     home_df['home_avg_goals_conceded'] = home_df.groupby('home_team')['home_goals_conceded'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  22.  
  23.     # Calculate current winning and losing streak for home teams
  24.     home_df['home_winning_streak'] = home_df.groupby('home_team')['home_wins'].transform(lambda x: x.groupby((x != x.shift()).cumsum()).cumcount() + 1)
  25.     home_df['home_losing_streak'] = home_df.groupby('home_team')['home_wins'].transform(lambda x: (1 - x).groupby((x != x.shift()).cumsum()).cumcount() + 1)
  26.  
  27.     # Calculate for away games
  28.     away_df = df.copy()
  29.     away_df['away_wins'] = away_df['home_win'].apply(lambda x: 1 if x == 0 else 0)  # Invert home_win for away team perspective
  30.     away_df['away_goals_scored'] = away_df['score_away']
  31.     away_df['away_goals_conceded'] = away_df['score_home']
  32.    
  33.     away_df.sort_values(['away_team', 'game_date'], inplace=True)
  34.  
  35.     # Rolling calculate win rate, goals scored, and goals conceded for away team
  36.     away_df['away_recent_win_rate'] = away_df.groupby('away_team')['away_wins'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  37.     away_df['away_avg_goals_scored'] = away_df.groupby('away_team')['away_goals_scored'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  38.     away_df['away_avg_goals_conceded'] = away_df.groupby('away_team')['away_goals_conceded'].transform(lambda x: x.rolling(window=games, min_periods=1).mean())
  39.  
  40.     # Calculate current winning and losing streak for away teams
  41.     away_df['away_winning_streak'] = away_df.groupby('away_team')['away_wins'].transform(lambda x: x.groupby((x != x.shift()).cumsum()).cumcount() + 1)
  42.     away_df['away_losing_streak'] = away_df.groupby('away_team')['away_wins'].transform(lambda x: (1 - x).groupby((x != x.shift()).cumsum()).cumcount() + 1)
  43.  
  44.     # Merge the metrics back to the original dataframe
  45.     df = df.merge(home_df[['game_date', 'home_team', 'home_recent_win_rate', 'home_avg_goals_scored', 'home_avg_goals_conceded', 'home_winning_streak', 'home_losing_streak']], on=['game_date', 'home_team'], how='left')
  46.     df = df.merge(away_df[['game_date', 'away_team', 'away_recent_win_rate', 'away_avg_goals_scored', 'away_avg_goals_conceded', 'away_winning_streak', 'away_losing_streak']], on=['game_date', 'away_team'], how='left')
  47.  
  48.     return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement