Cricket_data / pages /Player Stats.py
Adityaganesh's picture
Rename pages/Player Stats to pages/Player Stats.py
220b608 verified
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# Streamlit App Title
st.title("🏏 Cricket Data Analysis πŸ“Š")
# Load CSV Files using Streamlit Cache
@st.cache_data
def load_batting_data():
return pd.read_csv("Batting_10_Teams_Final.csv")
@st.cache_data
def load_bowling_data():
return pd.read_csv("Bowling_10_Teams_Final.csv")
# Load Data
try:
batting_df = load_batting_data()
bowling_df = load_bowling_data()
except FileNotFoundError as e:
st.error(f"❌ File not found: {e}")
st.stop()
# Main Page Filters
selected_country = st.selectbox("Select Country", sorted(batting_df["Country"].unique()))
option = st.selectbox("Choose the Player", sorted(batting_df[batting_df["Country"] == selected_country]["player_name"].unique()))
selected_format = st.selectbox("Select Format", ["All"] + list(batting_df["Format"].unique()) + ["IPL"])
st.write("You selected:", option)
# Filter Data
filtered_batting_df = batting_df[(batting_df["player_name"] == option) & (batting_df["Country"] == selected_country)]
filtered_bowling_df = bowling_df[(bowling_df["player_name"] == option) & (bowling_df["Country"] == selected_country)]
if selected_format != "All":
filtered_batting_df = filtered_batting_df[filtered_batting_df["Format"] == selected_format]
filtered_bowling_df = filtered_bowling_df[filtered_bowling_df["Format"] == selected_format]
# Convert NaN values to zero
filtered_batting_df = filtered_batting_df.fillna(0)
filtered_bowling_df = filtered_bowling_df.fillna(0)
# Drop Unnecessary Columns
display_batting_df = filtered_batting_df.drop(columns=["player_name", "Country"]).reset_index(drop=True)
display_bowling_df = filtered_bowling_df.drop(columns=["player_name", "Country"]).reset_index(drop=True)
# Display Raw Data
st.subheader(f"🏏 Batting Stats - {option}")
st.dataframe(display_batting_df)
st.subheader(f"🎯 Bowling Stats - {option}")
st.dataframe(display_bowling_df)
# Batting Efficiency
filtered_batting_df["Balls per Four"] = filtered_batting_df["Balls"] / filtered_batting_df["Fours"]
filtered_batting_df["Balls per Six"] = filtered_batting_df["Balls"] / filtered_batting_df["Sixes"]
# Bowling Efficiency
filtered_bowling_df["Balls per Wicket"] = filtered_bowling_df["Balls"] / filtered_bowling_df["Wickets"]
# Batting Visualizations
st.subheader(f"🏏 Boundary Efficiency - {option}")
fig = px.bar(filtered_batting_df, x="Format", y="Balls per Four", title="Balls Taken per Four Across Formats", color="Format")
st.plotly_chart(fig)
fig = px.bar(filtered_batting_df, x="Format", y="Balls per Six", title="Balls Taken per Six Across Formats", color="Format")
st.plotly_chart(fig)
# Bowling Visualizations
st.subheader(f"🎯 Bowling Efficiency - {option}")
fig = px.bar(filtered_bowling_df, x="Format", y="Eco", title="Bowling Economy Across Formats", color="Format")
st.plotly_chart(fig)
fig = px.bar(filtered_bowling_df, x="Format", y="Balls per Wicket", title="Balls Taken per Wicket Across Formats", color="Format")
st.plotly_chart(fig)