Cricket_playground / pages /1_player_stats.py
zoya23's picture
Rename pages/player_stats.py to pages/1_player_stats.py
704dc71 verified
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
# Load the dataset
df = pd.read_csv("cric_final.csv")
st.title("🏏 Player Performance Dashboard")
# Dropdown to Select Player
selected_player = st.selectbox("Select a Player", df["Player"].unique())
# Get Player Data
player_data = df[df["Player"] == selected_player]
if not player_data.empty:
# Replace None/NaN with "-" only in batting and bowling columns
batting_cols = [col for col in df.columns if "batting" in col.lower()]
bowling_cols = [col for col in df.columns if "bowling" in col.lower()]
player_data[batting_cols] = player_data[batting_cols].fillna("-")
player_data[bowling_cols] = player_data[bowling_cols].fillna("-")
# Only 2 tabs now: Batting Performance and Bowling Performance
tab1, tab2 = st.tabs(["πŸ“ˆ Batting Performance", "🎯 Bowling Performance"])
with tab1:
st.write("### πŸ“Š Batting Performance")
# Bar Chart for Batting Runs
fig, ax = plt.subplots(figsize=(7, 5))
sns.barplot(
x=["Test", "ODI", "T20", "IPL"],
y=player_data.iloc[0][["batting_Runs_Test", "batting_Runs_ODI", "batting_Runs_T20", "batting_Runs_IPL"]],
palette="magma",
ax=ax
)
ax.set_ylabel("Total Runs", fontsize=14)
ax.set_title(f"Batting Performance of {selected_player}", fontsize=16)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)
st.pyplot(fig)
# Side-by-Side Bar Chart for 50s & 100s
st.write("### Half-Centuries (50s) vs Centuries (100s)")
fig, ax = plt.subplots(figsize=(7, 5))
x_labels = ["Test", "ODI", "T20", "IPL"]
x = range(len(x_labels))
fifties = player_data.iloc[0][["batting_50s_Test", "batting_50s_ODI", "batting_50s_T20", "batting_50s_IPL"]]
hundreds = player_data.iloc[0][["batting_100s_Test", "batting_100s_ODI", "batting_100s_T20", "batting_100s_IPL"]]
width = 0.4
ax.bar([i - width/2 for i in x], fifties, width=width, label="50s", color="skyblue")
ax.bar([i + width/2 for i in x], hundreds, width=width, label="100s", color="orange")
ax.set_xticks(x)
ax.set_xticklabels(x_labels)
ax.set_ylabel("Count", fontsize=14)
ax.set_title(f"50s vs 100s of {selected_player}", fontsize=16)
ax.legend()
ax.grid(axis="y", linestyle="--", alpha=0.7)
st.pyplot(fig)
# Pie Chart for Batting Runs Distribution
st.write("### Batting Runs Distribution")
runs = player_data.iloc[0][["batting_Runs_Test", "batting_Runs_ODI", "batting_Runs_T20", "batting_Runs_IPL"]]
labels = ["Test", "ODI", "T20", "IPL"]
fig_pie = px.pie(values=runs, names=labels, title="Batting Runs Distribution", color=labels)
st.plotly_chart(fig_pie)
# Line Chart for Batting Averages
st.write("### Batting Averages Over Different Formats")
batting_averages = player_data.iloc[0][["batting_Average_Test", "batting_Average_ODI", "batting_Average_T20", "batting_Average_IPL"]]
fig_line, ax = plt.subplots(figsize=(7, 5))
ax.plot(["Test", "ODI", "T20", "IPL"], batting_averages, marker='o', color='tab:blue')
ax.set_ylabel("Batting Average", fontsize=14)
ax.set_title(f"Batting Averages of {selected_player}", fontsize=16)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)
st.pyplot(fig_line)
with tab2:
st.write("### 🎯 Bowling Performance")
# Bar Chart for Bowling Wickets
fig, ax = plt.subplots(figsize=(7, 5))
sns.barplot(
x=["Test", "ODI", "T20", "IPL"],
y=player_data.iloc[0][["bowling_Test_Wickets", "bowling_ODI_Wickets", "bowling_T20_Wickets", "bowling_IPL_Wickets"]],
palette="coolwarm",
ax=ax
)
ax.set_ylabel("Total Wickets", fontsize=14)
ax.set_title(f"Bowling Performance of {selected_player}", fontsize=16)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)
st.pyplot(fig)
# Pie Chart for Bowling Wickets Distribution
st.write("### Bowling Wickets Distribution")
wickets = player_data.iloc[0][["bowling_Test_Wickets", "bowling_ODI_Wickets", "bowling_T20_Wickets", "bowling_IPL_Wickets"]]
labels = ["Test", "ODI", "T20", "IPL"]
fig_pie_bowl = px.pie(values=wickets, names=labels, title="Bowling Wickets Distribution", color=labels)
st.plotly_chart(fig_pie_bowl)
# Line Chart for Bowling Average
st.write("### Bowling Averages Over Different Formats")
bowling_averages = player_data.iloc[0][["bowling_Test_Avg", "bowling_ODI_Avg", "bowling_T20_Avg", "bowling_IPL_Avg"]]
fig_line_bowl, ax = plt.subplots(figsize=(7, 5))
ax.plot(["Test", "ODI", "T20", "IPL"], bowling_averages, marker='o', color='tab:green')
ax.set_ylabel("Bowling Average", fontsize=14)
ax.set_title(f"Bowling Averages of {selected_player}", fontsize=16)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)
st.pyplot(fig_line_bowl)
else:
st.warning("⚠️ Player not found in the dataset.")