nesticot commited on
Commit
363077f
·
verified ·
1 Parent(s): 33acbf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -686,6 +686,7 @@ def server(input, output, session):
686
 
687
  # Define the columns to subtract
688
  cols_to_subtract = [
 
689
  ("start_speed", "start_speed_old"),
690
  ("max_start_speed", "max_start_speed_old"),
691
  ("ivb", "ivb_old"),
@@ -720,19 +721,44 @@ def server(input, output, session):
720
  ])
721
 
722
 
723
-
724
-
725
-
726
-
727
- percent_cols = ['pitch_percent', 'rhh_percent', 'lhh_percent']
728
-
 
729
  df_merge = df_merge.with_columns([
730
- (pl.col(col) * 100) # Convert to percentage
731
- .round(1) # Round to 1 decimal
732
- .map_elements(lambda x: f"{x:.1f}%") # Format as string with '%'
733
- .alias(col + "_formatted")
734
- for col in percent_cols
735
- ]).sort(['pitcher_id','count'],descending=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736
 
737
 
738
  columns = [
 
686
 
687
  # Define the columns to subtract
688
  cols_to_subtract = [
689
+
690
  ("start_speed", "start_speed_old"),
691
  ("max_start_speed", "max_start_speed_old"),
692
  ("ivb", "ivb_old"),
 
721
  ])
722
 
723
 
724
+ cols_to_subtract_percent = [
725
+ ("pitch_percent", "pitch_percent_old"),
726
+ ("rhh_percent", "rhh_percent_old"),
727
+ ("lhh_percent", "lhh_percent_old"),
728
+ ]
729
+
730
+
731
  df_merge = df_merge.with_columns([
732
+ # Step 1: Create _diff columns with the default value (e.g., 80) if old is null
733
+ pl.when(pl.col(old).is_null())
734
+ .then(pl.lit(10000)) # If old is null, assign 80 as the default
735
+ .otherwise(pl.col(new) - pl.col(old)) # Otherwise subtract old from new
736
+ .alias(new + "_diff")
737
+ for new, old in cols_to_subtract_percent
738
+ ])
739
+
740
+ # percent_cols = ['pitch_percent', 'rhh_percent', 'lhh_percent']
741
+
742
+ # Step 2: Format the columns with (value (+diff)) - exclude brackets if diff is 80
743
+ df_merge = df_merge.with_columns([
744
+ pl.when(pl.col(new + "_diff").eq(10000)) # If diff is 80, no need to include brackets
745
+ .then(
746
+ (pl.col(new)*100).round(1).map_elements(lambda x: f"{x:.1f}%").cast(pl.Utf8) +
747
+ "\n(" +
748
+ (pl.col(new)*100).round(1)
749
+ .map_elements(lambda x: f"{x:+.1f}%") +
750
+ ")"
751
+ )
752
+ .otherwise(
753
+ (pl.col(new)*100).round(1).map_elements(lambda x: f"{x:.1f}%").cast(pl.Utf8) +
754
+ "\n(" +
755
+ (pl.col(new + "_diff")*100).round(1)
756
+ .map_elements(lambda x: f"{x:+.1f}%") +
757
+ ")"
758
+ ).alias(new + "_formatted")
759
+ for new, _ in cols_to_subtract_percent
760
+ ])
761
+
762
 
763
 
764
  columns = [