Adityaganesh commited on
Commit
6396724
Β·
verified Β·
1 Parent(s): 46b6e00

Update pages/Player Comparision.py

Browse files
Files changed (1) hide show
  1. pages/Player Comparision.py +31 -16
pages/Player Comparision.py CHANGED
@@ -2,7 +2,6 @@ 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
@@ -21,26 +20,39 @@ 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)
@@ -48,7 +60,7 @@ bat2_avg = get_avg(bat2, batting_metrics)
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))
@@ -56,10 +68,13 @@ 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)
 
 
 
 
2
  import pandas as pd
3
  import plotly.graph_objects as go
4
 
 
5
  st.title("🀝 Cricket Player Comparison")
6
 
7
  # Load data with caching
 
20
  st.error(f"File not found: {e}")
21
  st.stop()
22
 
23
+ # Show bowling columns to debug (optional)
24
+ # st.write("Bowling columns:", bowling_df.columns.tolist())
25
+
26
+ # Select two players for comparison
27
  player1 = st.selectbox("Select First Player", sorted(batting_df["player_name"].unique()), key="p1")
28
  player2 = st.selectbox("Select Second Player", sorted(batting_df["player_name"].unique()), key="p2")
29
 
30
+ # Filter batting data and fill NaNs
31
  bat1 = batting_df[batting_df["player_name"] == player1].fillna(0)
32
  bat2 = batting_df[batting_df["player_name"] == player2].fillna(0)
33
 
34
+ # Filter bowling data and fill NaNs
35
  bowl1 = bowling_df[bowling_df["player_name"] == player1].fillna(0)
36
  bowl2 = bowling_df[bowling_df["player_name"] == player2].fillna(0)
37
 
38
+ # Batting metrics based on your CSV columns
39
  batting_metrics = ["Runs", "Average", "SR", "Fours", "Sixes"]
 
40
 
41
+ # Bowling metrics - check which columns exist in bowling_df before using
42
+ possible_bowling_metrics = ["Wickets", "Eco", "Average", "SR"]
43
+ bowling_metrics = [col for col in possible_bowling_metrics if col in bowling_df.columns]
44
+
45
+ # Function to safely get average values
46
  def get_avg(df, cols):
47
+ existing_cols = [c for c in cols if c in df.columns]
48
+ if df.empty or not existing_cols:
49
  return pd.Series({col: 0 for col in cols})
50
+ avg_series = df[existing_cols].mean()
51
+ # Fill missing cols with zero
52
+ for c in cols:
53
+ if c not in avg_series:
54
+ avg_series[c] = 0
55
+ return avg_series[cols]
56
 
57
  bat1_avg = get_avg(bat1, batting_metrics)
58
  bat2_avg = get_avg(bat2, batting_metrics)
 
60
  bowl1_avg = get_avg(bowl1, bowling_metrics)
61
  bowl2_avg = get_avg(bowl2, bowling_metrics)
62
 
63
+ # Batting Comparison Plot
64
  st.subheader("🏏 Batting Stats Comparison")
65
  fig_bat = go.Figure()
66
  fig_bat.add_trace(go.Bar(x=batting_metrics, y=bat1_avg, name=player1))
 
68
  fig_bat.update_layout(barmode='group', yaxis_title='Value', title='Batting Metrics Comparison')
69
  st.plotly_chart(fig_bat)
70
 
71
+ # Bowling Comparison Plot (only if bowling_metrics exist)
72
+ if bowling_metrics:
73
+ st.subheader("🎯 Bowling Stats Comparison")
74
+ fig_bowl = go.Figure()
75
+ fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl1_avg, name=player1))
76
+ fig_bowl.add_trace(go.Bar(x=bowling_metrics, y=bowl2_avg, name=player2))
77
+ fig_bowl.update_layout(barmode='group', yaxis_title='Value', title='Bowling Metrics Comparison')
78
+ st.plotly_chart(fig_bowl)
79
+ else:
80
+ st.info("Bowling stats columns missing or not found in the dataset.")