Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import plotly.graph_objects as go | |
| st.title("π€ Cricket Player Comparison") | |
| # Load data with caching | |
| def load_batting_data(): | |
| return pd.read_csv("Batting_10_Teams_Final.csv") | |
| def load_bowling_data(): | |
| return pd.read_csv("Bowling_10_Teams_Final.csv") | |
| try: | |
| batting_df = load_batting_data() | |
| bowling_df = load_bowling_data() | |
| except FileNotFoundError as e: | |
| st.error(f"File not found: {e}") | |
| st.stop() | |
| # Show bowling columns to debug (optional) | |
| # st.write("Bowling columns:", bowling_df.columns.tolist()) | |
| # Select two players for comparison | |
| player1 = st.selectbox("Select First Player", sorted(batting_df["player_name"].unique()), key="p1") | |
| player2 = st.selectbox("Select Second Player", sorted(batting_df["player_name"].unique()), key="p2") | |
| # Filter batting data and fill NaNs | |
| bat1 = batting_df[batting_df["player_name"] == player1].fillna(0) | |
| bat2 = batting_df[batting_df["player_name"] == player2].fillna(0) | |
| # Filter bowling data and fill NaNs | |
| bowl1 = bowling_df[bowling_df["player_name"] == player1].fillna(0) | |
| bowl2 = bowling_df[bowling_df["player_name"] == player2].fillna(0) | |
| # Batting metrics based on your CSV columns | |
| batting_metrics = ["Runs", "Average", "SR", "Fours", "Sixes"] | |
| # Bowling metrics - check which columns exist in bowling_df before using | |
| possible_bowling_metrics = ["Wickets", "Eco", "Average", "SR"] | |
| bowling_metrics = [col for col in possible_bowling_metrics if col in bowling_df.columns] | |
| # Function to safely get average values | |
| def get_avg(df, cols): | |
| existing_cols = [c for c in cols if c in df.columns] | |
| if df.empty or not existing_cols: | |
| return pd.Series({col: 0 for col in cols}) | |
| avg_series = df[existing_cols].mean() | |
| # Fill missing cols with zero | |
| for c in cols: | |
| if c not in avg_series: | |
| avg_series[c] = 0 | |
| return avg_series[cols] | |
| bat1_avg = get_avg(bat1, batting_metrics) | |
| bat2_avg = get_avg(bat2, batting_metrics) | |
| bowl1_avg = get_avg(bowl1, bowling_metrics) | |
| bowl2_avg = get_avg(bowl2, bowling_metrics) | |
| # Batting Comparison Plot | |
| st.subheader("π Batting Stats Comparison") | |
| fig_bat = go.Figure() | |
| fig_bat.add_trace(go.Bar(x=batting_metrics, y=bat1_avg, name=player1)) | |
| fig_bat.add_trace(go.Bar(x=batting_metrics, y=bat2_avg, name=player2)) | |
| fig_bat.update_layout(barmode='group', yaxis_title='Value', title='Batting Metrics Comparison') | |
| st.plotly_chart(fig_bat) | |
| # Bowling Comparison Plot (only if bowling_metrics exist) | |
| if bowling_metrics: | |
| st.subheader("π― Bowling Stats Comparison") | |
| fig_bowl = go.Figure() | |
| fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl1_avg, name=player1)) | |
| fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl2_avg, name=player2)) | |
| fig_bowl.update_layout(barmode='group', yaxis_title='Value', title='Bowling Metrics Comparison') | |
| st.plotly_chart(fig_bowl) | |
| else: | |
| st.info("Bowling stats columns missing or not found in the dataset.") | |