Jon Solow commited on
Commit
a4ba037
1 Parent(s): 8de66dc

Allow for grouping of team formations

Browse files
src/pages/9_Team_Formations.py CHANGED
@@ -1,4 +1,5 @@
1
  import datetime
 
2
  import streamlit as st
3
 
4
  from config import DEFAULT_ICON
@@ -10,7 +11,6 @@ from queries.nflverse.github_data import get_pbp_participation, get_current_tabl
10
 
11
  def load_data():
12
  data = get_pbp_participation(YEAR)
13
- # data = data[data.fantasy_position]
14
  teams_list = sorted(filter(None, data.possession_team.unique()))
15
  # position_list = data.position.unique()
16
  # weeks_list = sorted(data.week.unique())
@@ -30,8 +30,9 @@ def get_page():
30
  data, teams_list, data_load_time_str = load_data()
31
  st.write(f"Data loaded as of: {data_load_time_str} UTC")
32
  default_groups = [
33
- "possession_team",
34
- "offense_formation",
 
35
  ]
36
  group_options = [
37
  "week",
@@ -49,34 +50,32 @@ def get_page():
49
  "defense_personnel",
50
  ]
51
  group_by_selected = st.multiselect("Group by:", group_options) or default_groups
52
- teams_selected = st.multiselect("Team:", teams_list, placeholder="Select a team to filter") or teams_list
 
 
 
 
 
 
 
53
 
54
  with st.container():
55
- filtered_data = data[(data.possession_team.isin(teams_selected))]
56
- grouped_data = filtered_data.groupby(group_by_selected).size()
 
 
 
 
57
  st.dataframe(
58
- grouped_data,
59
- hide_index=False,
60
- # height=35 * (len(filtered_data) + 1) + 12,
 
 
 
 
 
61
  use_container_width=False,
62
- # column_order=[
63
- # "season",
64
- # "game_type",
65
- # "week",
66
- # "player",
67
- # "position",
68
- # "team",
69
- # "opponent",
70
- # "offense_snaps",
71
- # "offense_pct",
72
- # "defense_snaps",
73
- # "defense_pct",
74
- # "st_snaps",
75
- # "st_pct",
76
- # ],
77
- column_config={
78
- "0": st.column_config.NumberColumn(label="Count"),
79
- },
80
  )
81
 
82
 
 
1
  import datetime
2
+ import pandas as pd
3
  import streamlit as st
4
 
5
  from config import DEFAULT_ICON
 
11
 
12
  def load_data():
13
  data = get_pbp_participation(YEAR)
 
14
  teams_list = sorted(filter(None, data.possession_team.unique()))
15
  # position_list = data.position.unique()
16
  # weeks_list = sorted(data.week.unique())
 
30
  data, teams_list, data_load_time_str = load_data()
31
  st.write(f"Data loaded as of: {data_load_time_str} UTC")
32
  default_groups = [
33
+ "down",
34
+ "play_type",
35
+ "offense_personnel",
36
  ]
37
  group_options = [
38
  "week",
 
50
  "defense_personnel",
51
  ]
52
  group_by_selected = st.multiselect("Group by:", group_options) or default_groups
53
+ team_selected = st.selectbox("Team:", teams_list)
54
+ week_selection = st.slider(
55
+ "Filter Week Range:",
56
+ min_value=data["week"].min(),
57
+ max_value=data["week"].max(),
58
+ value=(data["week"].min(), data["week"].max()),
59
+ step=1,
60
+ )
61
 
62
  with st.container():
63
+ filtered_data = data[
64
+ (data.possession_team == team_selected)
65
+ & (data.play_type.isin(["pass", "run"]))
66
+ & (data["week"].between(*week_selection))
67
+ ]
68
+
69
  st.dataframe(
70
+ pd.pivot_table(
71
+ filtered_data,
72
+ values="count_col",
73
+ index=group_by_selected,
74
+ columns="week",
75
+ aggfunc={"count_col": "sum"},
76
+ # margins=True,
77
+ ),
78
  use_container_width=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  )
80
 
81
 
src/queries/nflverse/github_data.py CHANGED
@@ -52,10 +52,12 @@ def get_pbp_participation(season_int: int) -> pd.DataFrame:
52
  , b.play_type
53
  , b.pass_length
54
  , b.pass_location
 
55
  from pbp_participation_pbp_participation_{season_int} a
56
  left join pbp_play_by_play_{season_int} b
57
  on a.play_id = b.play_id
58
  and a.nflverse_game_id = b.game_id
 
59
  """
60
  ).df()
61
  return df
 
52
  , b.play_type
53
  , b.pass_length
54
  , b.pass_location
55
+ , 1 as count_col
56
  from pbp_participation_pbp_participation_{season_int} a
57
  left join pbp_play_by_play_{season_int} b
58
  on a.play_id = b.play_id
59
  and a.nflverse_game_id = b.game_id
60
+ where b.week is not null
61
  """
62
  ).df()
63
  return df