Spaces:
Build error
Build error
Create Player Comparision
Browse files- pages/Player Comparision +65 -0
pages/Player Comparision
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
# Title
|
| 6 |
+
st.title("🤝 Cricket Player Comparison")
|
| 7 |
+
|
| 8 |
+
# Load data with caching
|
| 9 |
+
@st.cache_data
|
| 10 |
+
def load_batting_data():
|
| 11 |
+
return pd.read_csv("Batting_10_Teams_Final.csv")
|
| 12 |
+
|
| 13 |
+
@st.cache_data
|
| 14 |
+
def load_bowling_data():
|
| 15 |
+
return pd.read_csv("Bowling_10_Teams_Final.csv")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
batting_df = load_batting_data()
|
| 19 |
+
bowling_df = load_bowling_data()
|
| 20 |
+
except FileNotFoundError as e:
|
| 21 |
+
st.error(f"File not found: {e}")
|
| 22 |
+
st.stop()
|
| 23 |
+
|
| 24 |
+
# Player selectors
|
| 25 |
+
player1 = st.selectbox("Select First Player", sorted(batting_df["player_name"].unique()), key="p1")
|
| 26 |
+
player2 = st.selectbox("Select Second Player", sorted(batting_df["player_name"].unique()), key="p2")
|
| 27 |
+
|
| 28 |
+
# Filter player data and fill NaNs
|
| 29 |
+
bat1 = batting_df[batting_df["player_name"] == player1].fillna(0)
|
| 30 |
+
bat2 = batting_df[batting_df["player_name"] == player2].fillna(0)
|
| 31 |
+
|
| 32 |
+
bowl1 = bowling_df[bowling_df["player_name"] == player1].fillna(0)
|
| 33 |
+
bowl2 = bowling_df[bowling_df["player_name"] == player2].fillna(0)
|
| 34 |
+
|
| 35 |
+
# Define stats to compare
|
| 36 |
+
batting_metrics = ["Runs", "Average", "SR", "Fours", "Sixes"]
|
| 37 |
+
bowling_metrics = ["Wickets", "Eco", "Average", "SR"]
|
| 38 |
+
|
| 39 |
+
# Calculate averages or zeros if no data
|
| 40 |
+
def get_avg(df, cols):
|
| 41 |
+
if df.empty:
|
| 42 |
+
return pd.Series({col: 0 for col in cols})
|
| 43 |
+
return df[cols].mean()
|
| 44 |
+
|
| 45 |
+
bat1_avg = get_avg(bat1, batting_metrics)
|
| 46 |
+
bat2_avg = get_avg(bat2, batting_metrics)
|
| 47 |
+
|
| 48 |
+
bowl1_avg = get_avg(bowl1, bowling_metrics)
|
| 49 |
+
bowl2_avg = get_avg(bowl2, bowling_metrics)
|
| 50 |
+
|
| 51 |
+
# Batting comparison plot
|
| 52 |
+
st.subheader("🏏 Batting Stats Comparison")
|
| 53 |
+
fig_bat = go.Figure()
|
| 54 |
+
fig_bat.add_trace(go.Bar(x=batting_metrics, y=bat1_avg, name=player1))
|
| 55 |
+
fig_bat.add_trace(go.Bar(x=batting_metrics, y=bat2_avg, name=player2))
|
| 56 |
+
fig_bat.update_layout(barmode='group', yaxis_title='Value', title='Batting Metrics Comparison')
|
| 57 |
+
st.plotly_chart(fig_bat)
|
| 58 |
+
|
| 59 |
+
# Bowling comparison plot
|
| 60 |
+
st.subheader("🎯 Bowling Stats Comparison")
|
| 61 |
+
fig_bowl = go.Figure()
|
| 62 |
+
fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl1_avg, name=player1))
|
| 63 |
+
fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl2_avg, name=player2))
|
| 64 |
+
fig_bowl.update_layout(barmode='group', yaxis_title='Value', title='Bowling Metrics Comparison')
|
| 65 |
+
st.plotly_chart(fig_bowl)
|