Spaces:
Sleeping
Sleeping
File size: 29,718 Bytes
c0b09c5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
st.set_page_config(page_icon='π', layout='wide', page_title='Cricket Analysis')
st.title("π Cricket Stats Dashboard")
# Load dataset
df_batting = pd.read_csv("Batting_10_Teams_Final.csv", on_bad_lines='skip')
df_bowling = pd.read_csv("new_Bowling_10_Teams_Final.csv", on_bad_lines='skip')
col1, col2 = st.columns(2)
# Select country
with col1:
country = sorted(df_batting["Country"].unique().tolist())
selected_country = st.selectbox("Select a Country", country)
# Filter players based on selected country
filtered_players = df_batting[df_batting["Country"] == selected_country]["player_name"].unique()
# Select player
with col2:
players = ["All"] + sorted(filtered_players.tolist())
selected_player = st.selectbox("Select a Player", players)
selected_format = 'All'
# π Display selected filters
st.write(f"π You selected: **{selected_country}** β **{selected_player}**")
# β
Filtering Data
filtered_batting_df = df_batting[df_batting["Country"] == selected_country]
filtered_bowling_df = df_bowling[df_bowling["Country"] == selected_country]
# Apply player filter only if a specific player is selected
if selected_player != "All":
filtered_batting_df = filtered_batting_df[filtered_batting_df["player_name"] == selected_player]
filtered_bowling_df = filtered_bowling_df[filtered_bowling_df["player_name"] == selected_player]
grouped_batting_df = filtered_batting_df.groupby("Format").sum().reset_index()
grouped_bowling_df = filtered_bowling_df.groupby("Format").sum().reset_index()
if not grouped_batting_df.empty and not grouped_bowling_df.empty:
st.write("### π Batting & Bowling Stats")
# Create Tabs for Each Format
tab_list = st.tabs(["Overall"] + grouped_batting_df["Format"].tolist()) # Add "Overall" tab
# π Overall Stats
overall_batting = filtered_batting_df.sum(numeric_only=True)
overall_bowling = filtered_bowling_df.sum(numeric_only=True)
with tab_list[0]: # Overall tab
st.subheader("Overall Stats")
col1, col9, col2 = st.columns([1,0.05,1])
# Batting Stats
with col1:
st.subheader("π Batting Stats")
c1, c2, c3 = st.columns(3)
with c1:
st.metric("Total Matches", int(overall_batting["Matches"]))
st.metric("Total Innings", int(overall_batting["Innings"]))
st.metric("Strike rate", overall_batting["SR"])
with c2:
st.metric("Total Runs", int(overall_batting["Runs"]))
st.metric("Average Runs", overall_batting["Average"])
st.metric("Total Fours", int(overall_batting["Fours"]))
st.metric("Total Sixes", int(overall_batting["Sixes"]))
with c3:
st.metric("Total 100s", int(overall_batting["100s"]))
st.metric("Total 50s", int(overall_batting["50s"]))
if not overall_batting.empty:
# Conditional Strike Rate Trend Plot
if selected_player == "All":
# Average Strike Rate Across Formats (for all players)
format_sr = filtered_batting_df.groupby("Format")["SR"].mean().reset_index()
fig1 = px.line(
format_sr, x="Format", y="SR",
title="Average Strike Rate Across Formats",
markers=True,
template="plotly_dark",
line_shape="spline",
color_discrete_sequence=["#00CFFF"]
)
fig1.update_traces(marker=dict(size=10, symbol="circle"))
fig1.update_layout(
title_font=dict(size=18, family="Arial"),
xaxis_title="Match Format",
yaxis_title="Strike Rate",
hovermode="x unified"
)
st.plotly_chart(fig1, key="plot_strike_rate_trend")
else:
# Strike Rate Trend for Selected Player
player_sr = filtered_batting_df[filtered_batting_df["player_name"] == selected_player]
# Ensure player has strike rate data
if not player_sr.empty:
fig1_player = px.line(
player_sr, x="Format", y="SR",
title=f"{selected_player}: Strike Rate Across Formats",
markers=True,
template="plotly_dark",
line_shape="spline",
color_discrete_sequence=["#00CFFF"]
)
fig1_player.update_traces(marker=dict(size=10, symbol="circle"))
fig1_player.update_layout(
title_font=dict(size=18, family="Arial"),
xaxis_title="Match Format",
yaxis_title="Strike Rate",
hovermode="x unified"
)
st.plotly_chart(fig1_player, key="plot_player_sr")
# Conditional Plots Based on Player Selection
if selected_player == "All":
# Top 10 Players by Runs
top_10_batsmen = filtered_batting_df.drop_duplicates(subset=["player_name"]).nlargest(10, "Runs")
top_players_runs = filtered_batting_df[filtered_batting_df["player_name"].isin(top_10_batsmen["player_name"])]
fig2 = px.bar(
top_players_runs, x="player_name", y="Runs", color="Format",
title="Runs Across Formats for Top Players",
barmode="stack",
template="plotly_dark",
text_auto=True,
color_discrete_sequence=px.colors.qualitative.Safe
)
fig2.update_layout(
title_font=dict(size=18, family="Arial"),
xaxis_title="Player Name",
yaxis_title="Total Runs",
hovermode="x unified"
)
st.plotly_chart(fig2, key="plot_stacked_runs")
# Stacked Bar Chart: Sixes & Fours Across Formats for Top Batters
if selected_player == "All":
# Get Top 10 Batters Based on Sixes
ttop_10_batsmen = filtered_batting_df.drop_duplicates(subset=["player_name"]).nlargest(10, "Sixes")
top_batsmen_stats = filtered_batting_df[filtered_batting_df["player_name"].isin(top_10_batsmen["player_name"])]
# Aggregate Total Sixes & Fours per Player
top_batsmen_melted = top_batsmen_stats.groupby(["player_name"])[["Sixes", "Fours"]].sum().reset_index()
#print(top_batsmen_stats[['Format','player_name', 'Sixes','Fours']])
# Reshape Data for a Single Stacked Bar Chart
top_batsmen_melted = top_batsmen_melted.melt(
id_vars=["player_name"],
value_vars=["Sixes", "Fours"],
var_name="Boundary Type",
value_name="Count"
)
# Create Stacked Bar Chart with Facet for Each Format
fig3 = px.bar(
top_batsmen_melted,
x="player_name",
y="Count",
color="Boundary Type",
barmode="stack",
title="Sixes & Fours Across All Formats for Top Batters",
hover_data=["Boundary Type", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig3.update_layout(
xaxis_title="Player Name",
yaxis_title="Total Boundaries",
hovermode="x unified",
legend=dict(title="Boundary Type")
)
st.plotly_chart(fig3, key="plot_top_boundaries")
else:
# Get stats for the selected player across formats
player_stats = filtered_batting_df[filtered_batting_df["player_name"] == selected_player]
#print(player_stats[['Format','Sixes', 'Fours']])
# Ensure we reshape correctly to show both Sixes & Fours
player_stats_melted = player_stats.melt(
id_vars=["Format"],
value_vars=["Sixes", "Fours"],
var_name="Boundary Type",
value_name="Count"
)
#print(player_stats_melted)
# Create Stacked Bar Chart for Selected Player
fig4 = px.bar(
player_stats_melted,
x="Format", y="Count",
color="Boundary Type",
barmode="stack",
title=f"Sixes & Fours Across Formats for {selected_player}",
text_auto=True,
hover_data=["Boundary Type", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig4.update_layout(
xaxis_title="Match Format",
yaxis_title="Total Boundaries",
hovermode="x unified",
legend=dict(title="Boundary Type")
)
st.plotly_chart(fig4, key="plot_selected_player_boundaries")
# Pie Chart: Runs Contribution Across Formats
format_runs = filtered_batting_df.groupby("Format")["Runs"].sum().reset_index()
fig5 = px.pie(
format_runs, names="Format", values="Runs",
title="Runs Contribution Across Formats",
template="plotly_dark",
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig5.update_traces(
title_font=dict(size=18, family="Arial"),
textinfo="label+value",
pull=[0.01, 0.01, 0.01, 0.01]
)
st.plotly_chart(fig5, key="plot_runs_contribution")
st.markdown("---") # Divider
# **Vertical Line (Divider)**
with col9:
st.markdown(
"""
<style>
.divider {
height: auto;
width: 3px;
background-color: #555;
margin: auto;
}
</style>
<div class="divider"></div>
""",
unsafe_allow_html=True
)
# OverAll Bowling Stats
with col2:
st.subheader("π― Bowling Stats")
c1, c2, c3, c4 = st.columns(4)
with c1:
st.metric("Total Matches", int(overall_bowling["Matches"]))
st.metric("Total Innings", int(overall_bowling["Innings"]))
st.metric("Strike Rate", overall_bowling["SR"])
with c2:
st.metric("Total Wickets", int(overall_bowling["Wickets"]))
st.metric("Average Wickets", overall_bowling["Avg"])
st.metric("Total 4W", int(overall_bowling["4w"]))
st.metric("Total 5W", int(overall_bowling["5w"]))
with c3:
st.metric("Best BBI Runs", int(overall_bowling["BBI_Runs"]))
st.metric("Best BBI Wickets", int(overall_bowling["BBI_Wickets"]))
st.metric("Best BBM Runs", int(overall_bowling["BBM_Runs"]))
st.metric("Best BBM Wickets", int(overall_bowling["BBM_Wickets"]))
with c4:
st.metric("Maidens", int(overall_bowling["Maidens"]))
st.metric("Eco", overall_bowling["Eco"])
if not overall_bowling.empty:
# Line Chart: Economy Rate Trend Across Formats
format_stats = filtered_bowling_df.groupby("Format").agg({"Eco": "mean", "Matches": "sum"}).reset_index()
fig1 = make_subplots(specs=[[{"secondary_y": True}]]) # Dual axis plot
# Economy Rate Line
fig1.add_trace(
go.Scatter(x=format_stats["Format"], y=format_stats["Eco"],
mode="lines+markers", name="Economy Rate", line_shape="spline",
marker=dict(size=10, symbol="circle", color="#FF6F61")),
secondary_y=False
)
# Number of Matches Bar
fig1.add_trace(
go.Bar(x=format_stats["Format"], y=format_stats["Matches"],
name="No. of Matches", marker_color="lightblue", opacity=0.6),
secondary_y=True
)
fig1.update_layout(
title="Economy Rate & Matches Across Formats",
xaxis_title="Match Format",
yaxis=dict(title="Average Economy Rate"),
yaxis2=dict(title="Total Matches", overlaying="y", side="right"),
hovermode="x unified",
)
st.plotly_chart(fig1, key="plot_eco_trend")
# Stacked Bar Chart: Wickets & Matches Across Formats for Top Bowlers
if selected_player == "All":
top_10_bowlers = filtered_bowling_df.drop_duplicates(subset=["player_name"]).nlargest(10, "Wickets")
top_bowler_stats = filtered_bowling_df[filtered_bowling_df["player_name"].isin(top_10_bowlers["player_name"])]
fig2 = px.bar(
top_bowler_stats, x="player_name", y="Wickets",
color="Format", barmode="stack",
title="Wickets Across Formats for Top Bowlers",
text_auto=True,
hover_data=["Wickets", "Format"],
color_discrete_sequence=px.colors.qualitative.Safe
)
fig2.update_layout(
xaxis_title="Player Name",
yaxis_title="Total Wickets",
hovermode="x unified",
legend=dict(title="Match Format")
)
st.plotly_chart(fig2, key="plot_top_bowlers")
# Stacked Bar Chart: Wickets & Matches Across Formats for Top Bowlers
if selected_player == "All":
top_10_bowlers = filtered_bowling_df.drop_duplicates(subset=["player_name"]).nlargest(10, "Wickets")
top_bowler_stats = filtered_bowling_df[filtered_bowling_df["player_name"].isin(top_10_bowlers["player_name"])]
# Aggregate Total Wickets & Maidens per Player
top_bowlers_melted = top_bowler_stats.groupby(["player_name"])[["Wickets", "Maidens"]].sum().reset_index()
# Reshape Data for Stacked Bar Chart
top_bowlers_melted = top_bowlers_melted.melt(
id_vars=["player_name"],
value_vars=["Wickets", "Maidens"],
var_name="Bowling Stat",
value_name="Count"
)
# Create Stacked Bar Chart with Facet for Each Format
fig_bowling = px.bar(
top_bowlers_melted,
x="player_name", y="Count",
color="Bowling Stat",
barmode="stack",
title="Wickets & Maidens Across Formats for Top Bowlers",
hover_data=["Bowling Stat", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig_bowling.update_layout(
xaxis_title="Player Name",
yaxis_title="Total Count",
hovermode="x unified",
legend=dict(title="Bowling Stat")
)
st.plotly_chart(fig_bowling, key="plot_top_bowling_stats")
else:
# Get stats for the selected player across formats
player_bowling_stats = filtered_bowling_df[filtered_bowling_df["player_name"] == selected_player]
# Ensure we reshape correctly to show both Wickets & Maidens
player_bowling_melted = player_bowling_stats.melt(
id_vars=["Format"],
value_vars=["Wickets", "Maidens"],
var_name="Bowling Stat",
value_name="Count"
)
# Create Stacked Bar Chart for Selected Player
fig_player_bowling = px.bar(
player_bowling_melted,
x="Format", y="Count",
color="Bowling Stat",
barmode="stack",
title=f"Wickets & Maidens Across Formats for {selected_player}",
text_auto=True,
hover_data=["Bowling Stat", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig_player_bowling.update_layout(
xaxis_title="Match Format",
yaxis_title="Total Count",
hovermode="x unified",
legend=dict(title="Bowling Stat")
)
st.plotly_chart(fig_player_bowling, key="plot_selected_player_bowling_stats")
# Pie Chart: Wickets & Matches Contribution Across Formats (Sunburst)
format_totals = filtered_bowling_df.groupby("Format").agg({"Wickets": "sum", "Matches":"sum"}).reset_index()
fig3 = px.sunburst(
format_totals, path=["Format"], values="Wickets",
color = "Matches",
title="Wickets & Matches Contribution Across Formats",
color_continuous_scale=px.colors.qualitative.Pastel
)
# Customize hover info to display both Wickets & Matches
fig3.update_traces(
hovertemplate="<b>%{label}</b><br>Wickets: %{value}<br>Matches: %{customdata}<extra></extra>",
customdata=format_totals["Matches"],
textinfo = "label+value",
)
fig3.update_layout(
title_font=dict(size=18, family="Arial"),
hovermode="x unified",
legend=dict(title="Match Format"),
coloraxis_showscale=False
)
st.plotly_chart(fig3, key="plot_wickets_contribution")
# π **Format-wise Stats**
for i, format_name in enumerate(grouped_batting_df["Format"].unique()):
with tab_list[i + 1]: # Each format tab
st.subheader(f"{format_name} Stats")
col1, col3, col2 = st.columns([1, 0.05, 1])
# π **Batting Stats**
format_batting = grouped_batting_df[grouped_batting_df["Format"] == format_name]
if not format_batting.empty:
format_batting_row = format_batting.iloc[0]
with col1:
st.subheader("π Batting Stats")
c1, c2, c3 = st.columns(3)
with c1:
st.metric("Total Matches", int(format_batting_row["Matches"]))
st.metric("Total Innings", int(format_batting_row["Innings"]))
st.metric("Strike Rate", float(format_batting_row["SR"]))
with c2:
st.metric("Total Runs", int(format_batting_row["Runs"]))
st.metric("Average Runs", float(format_batting_row["Average"]))
st.metric("Total Fours", int(format_batting_row["Fours"]))
st.metric("Total Sixes", int(format_batting_row["Sixes"]))
with c3:
st.metric("Total 100s", int(format_batting_row["100s"]))
st.metric("Total 50s", int(format_batting_row["50s"]))
format_filtered_df = filtered_batting_df[filtered_batting_df["Format"] == format_name]
if not format_filtered_df.empty:
# Show only top 10 run-scorers
if selected_player != "All":
top_batters = format_filtered_df[format_filtered_df["player_name"] == selected_player]
else:
top_batters = format_filtered_df.nlargest(10, "Runs")
# Line Chart: Strike Rate of Top 10 Players
fig2 = px.line(
top_batters,
x="player_name", y="SR",
markers=True,
line_shape="spline",
title=f"{format_name}: Strike Rate of Top 10 Players",
)
st.plotly_chart(fig2, key=f"plot_{format_name}_batting_sr")
if selected_player == "All":
top_10_batsmen_format = format_filtered_df.drop_duplicates(subset=["player_name"]).nlargest(10, "Sixes")
top_batsmen_stats_format = format_filtered_df[format_filtered_df["player_name"].isin(top_10_batsmen_format["player_name"])]
# Aggregate Total Sixes & Fours per Player
top_batsmen_melted_format = top_batsmen_stats_format.groupby(["player_name"])[["Sixes", "Fours"]].sum().reset_index()
# Reshape Data
top_batsmen_melted_format = top_batsmen_melted_format.melt(
id_vars=["player_name"],
value_vars=["Sixes", "Fours"],
var_name="Boundary Type",
value_name="Count"
)
# Create Stacked Bar Chart for this format
fig_format = px.bar(
top_batsmen_melted_format,
x="player_name",
y="Count",
color="Boundary Type",
barmode="stack",
title=f"Sixes & Fours in {format_name} Matches (Top 10 Players)",
hover_data=["Boundary Type", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig_format.update_layout(
xaxis_title="Player Name",
yaxis_title="Total Boundaries",
hovermode="x unified",
legend=dict(title="Boundary Type")
)
st.plotly_chart(fig_format, key=f"plot_top_boundaries_{format_name}")
else:
# Get stats for the selected player in this format
player_stats_format = format_filtered_df[format_filtered_df["player_name"] == selected_player]
# Reshape Data
player_stats_melted_format = player_stats_format.melt(
id_vars=["Format"],
value_vars=["Sixes", "Fours"],
var_name="Boundary Type",
value_name="Count"
)
# Create Stacked Bar Chart for Selected Player in this format
fig_selected_format = px.bar(
player_stats_melted_format,
x="Format", y="Count",
color="Boundary Type",
barmode="group",
title=f"Sixes & Fours in {format_name} Matches for {selected_player}",
text_auto=True,
hover_data=["Boundary Type", "Count"],
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig_selected_format.update_layout(
xaxis_title="Match Format",
yaxis_title="Total Boundaries",
hovermode="x unified",
legend=dict(title="Boundary Type")
)
st.plotly_chart(fig_selected_format, key=f"plot_selected_player_boundaries_{format_name}")
# **Vertical Line (Divider)**
with col3:
st.markdown(
"""
<style>
.divider {
height: auto;
width: 3px;
background-color: #555;
margin: auto;
}
</style>
<div class="divider"></div>
""",
unsafe_allow_html=True
)
# π― **Bowling Stats**
format_bowling = grouped_bowling_df[grouped_bowling_df["Format"] == format_name]
if not format_bowling.empty:
format_bowling_row = format_bowling.iloc[0]
with col2:
st.subheader("π― Bowling Stats")
c1, c2, c3, c4 = st.columns(4)
with c1:
st.metric("Total Matches", int(format_bowling_row["Matches"]))
st.metric("Total Innings", int(format_bowling_row["Innings"]))
st.metric("Strike Rate", float(format_bowling_row["SR"]))
with c2:
st.metric("Total Wickets", int(format_bowling_row["Wickets"]))
st.metric("Average Wickets", float(format_bowling_row['Avg']))
st.metric("Total 4W", int(format_bowling_row["4w"]))
st.metric("Total 5W", int(format_bowling_row["5w"]))
with c3:
st.metric("Best BBI Runs", format_bowling_row["BBI_Runs"])
st.metric("Best BBI Wickets", format_bowling_row["BBI_Wickets"])
st.metric("Best BBM Runs", format_bowling_row["BBM_Runs"])
st.metric("Best BBM Wickets", format_bowling_row["BBM_Wickets"])
with c4:
st.metric("Maidens", int(format_bowling_row['Maidens']))
st.metric("Economy Rate", float(format_bowling_row['Eco']))
format_filtered_df2 = filtered_bowling_df[filtered_bowling_df["Format"] == format_name]
if not format_filtered_df2.empty:
if selected_player != "All":
top_bowlers = format_filtered_df2[format_filtered_df2["player_name"] == selected_player]
else:
top_bowlers = format_filtered_df2.nlargest(10, "Wickets")
# Bar Chart: Top 10 Wicket-Takers
fig_bowling_1 = px.bar(
top_bowlers,
x="player_name", y="Wickets",
color="Wickets",
title=f"{format_name}: {'Player' if selected_player != 'All' else 'Top 10'} Wicket-Takers",
text_auto=True
)
st.plotly_chart(fig_bowling_1, key=f"plot_{format_name}_bowling_wickets")
# π Line Plot: Economy Rate of Top 10 Bowlers
fig_bowling_2 = px.line(
top_bowlers,
x="player_name", y="Eco",
markers = True,
line_shape='spline',
title=f"π° {format_name}: {'Player' if selected_player != 'All' else 'Top 10'} Economy Rate",
)
st.plotly_chart(fig_bowling_2, key=f"plot_{format_name}_bowling_economy") |