CoolFace
Apppublic

Chittrarasu/Cricket-match-prediction-FastAPI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
data_clean.py46 linesDownload Raw Back to data
1import pandas as pd2 3# Load ball-by-ball dataset with low_memory=False to handle mixed types4ball_df = pd.read_csv('ODI_Match_Data.csv', low_memory=False)5 6# Clean ball-by-ball dataset7ball_df['match_id'] = ball_df['match_id'].astype(int)  # Ensure match_id is an integer8ball_df['season'] = ball_df['season'].astype(str).str.strip()  # Ensure season is a string, handle mixed types9ball_df['start_date'] = pd.to_datetime(ball_df['start_date'], errors='coerce')  # Convert to datetime, handle invalid dates10ball_df['runs_off_bat'] = ball_df['runs_off_bat'].fillna(0).astype(int)11ball_df['extras'] = ball_df['extras'].fillna(0).astype(int)12ball_df['wides'] = ball_df['wides'].fillna(0).astype(int)13ball_df['noballs'] = ball_df['noballs'].fillna(0).astype(int)14ball_df['byes'] = ball_df['byes'].fillna(0).astype(int)15ball_df['legbyes'] = ball_df['legbyes'].fillna(0).astype(int)16ball_df['penalty'] = ball_df['penalty'].fillna(0).astype(int)17ball_df['wicket_type'] = ball_df['wicket_type'].notna().astype(int)  # 1 if wicket, 0 if not18ball_df['batting_team'] = ball_df['batting_team'].astype(str).str.strip().str.title()  # Ensure string, then title case19ball_df['bowling_team'] = ball_df['bowling_team'].astype(str).str.strip().str.title()20ball_df['striker'] = ball_df['striker'].astype(str).str.strip().str.title()21ball_df['non_striker'] = ball_df['non_striker'].astype(str).str.strip().str.title()22ball_df['bowler'] = ball_df['bowler'].astype(str).str.strip().str.title()23ball_df['player_dismissed'] = ball_df['player_dismissed'].astype(str).str.strip().str.title()  # Ensure string, then title case24 25# Handle 'other_player_dismissed' - check if it's numeric or non-string, convert to string if possible26if ball_df['other_player_dismissed'].dtype != 'object':  # If not string type27    ball_df['other_player_dismissed'] = ball_df['other_player_dismissed'].astype(str).str.strip().str.title()28else:29    # If already object type, handle NaN or non-string values30    ball_df['other_player_dismissed'] = ball_df['other_player_dismissed'].fillna('').astype(str).str.strip().str.title()31 32ball_df['other_wicket_type'] = ball_df['other_wicket_type'].astype(str).str.strip()  # Ensure string, handle as is33 34# Extract venue if needed (assuming start_date might contain venue or it's separate)35if 'venue' not in ball_df.columns:36    if 'start_date' in ball_df.columns and ball_df['start_date'].dtype == 'object':37        ball_df['venue'] = ball_df['start_date'].str.extract(r', (.+)$').fillna('N/A')38    else:39        ball_df['venue'] = 'N/A'  # Default if venue isn’t available40 41# Calculate total runs (including extras)42ball_df['total_runs'] = ball_df['runs_off_bat'] + ball_df['extras']43 44# Save cleaned ball-by-ball dataset45ball_df.to_csv('cleaned_ball_data.csv', index=False)46print("Cleaned ball-by-ball dataset saved as 'cleaned_ball_data.csv'")