nesticot commited on
Commit
6d0bbe2
·
verified ·
1 Parent(s): 5ba0e36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -725,6 +725,21 @@ def server(input, output, session):
725
  pl.col("start_speed").count().alias("pitcher_total")
726
  )
727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
729
  pl.col('start_speed').count().alias('count'),
730
  pl.col('start_speed').mean().alias('start_speed'),
@@ -741,6 +756,7 @@ def server(input, output, session):
741
 
742
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
743
  df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id"], how="left")
 
744
 
745
  # Now calculate the pitch percent for each pitcher/pitch_type combination
746
  df_spring_group = df_spring_group.with_columns(
@@ -749,8 +765,8 @@ def server(input, output, session):
749
 
750
  # Optionally, if you want the percentage of left/right-handed batters within the group:
751
  df_spring_group = df_spring_group.with_columns([
752
- (pl.col("rhh_count") / pl.col("pitcher_total")).alias("rhh_percent"),
753
- (pl.col("lhh_count") / pl.col("pitcher_total")).alias("lhh_percent")
754
  ])
755
 
756
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
 
725
  pl.col("start_speed").count().alias("pitcher_total")
726
  )
727
 
728
+ df_pitcher_totals_hands = (
729
+ df_spring_stuff
730
+ .group_by(["pitcher_id", "batter_hand"])
731
+ .agg(pl.col("start_speed").count().alias("pitcher_total"))
732
+ .pivot(
733
+ values="pitcher_total",
734
+ index="pitcher_id",
735
+ columns="batter_hand",
736
+ aggregate_function="first"
737
+ )
738
+ .rename({"L": "pitcher_total_left", "R": "pitcher_total_right"})
739
+ .fill_null(0) # Fill missing values with 0 if some pitchers don't face both hands
740
+ )
741
+
742
+
743
  df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
744
  pl.col('start_speed').count().alias('count'),
745
  pl.col('start_speed').mean().alias('start_speed'),
 
756
 
757
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
758
  df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id"], how="left")
759
+ df_spring_group = df_spring_group.join(df_pitcher_totals_hands, on=["pitcher_id"], how="left")
760
 
761
  # Now calculate the pitch percent for each pitcher/pitch_type combination
762
  df_spring_group = df_spring_group.with_columns(
 
765
 
766
  # Optionally, if you want the percentage of left/right-handed batters within the group:
767
  df_spring_group = df_spring_group.with_columns([
768
+ (pl.col("rhh_count") / pl.col("pitcher_total_right")).alias("rhh_percent"),
769
+ (pl.col("lhh_count") / pl.col("pitcher_total_left")).alias("lhh_percent")
770
  ])
771
 
772
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')