Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -598,19 +598,22 @@ def server(input, output, session):
|
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
-
# List of desired columns
|
| 602 |
desired_columns = [
|
| 603 |
'Player ID', 'Batter', 'Bats', 'GP', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
| 604 |
'C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'DH', 'LHP', 'RHP'
|
| 605 |
]
|
| 606 |
|
| 607 |
-
#
|
| 608 |
-
|
|
|
|
|
|
|
|
|
|
| 609 |
|
| 610 |
-
#
|
| 611 |
-
df_test_merge = df_test_merge.select(
|
| 612 |
|
| 613 |
-
#
|
| 614 |
cols = {
|
| 615 |
('Player Info', 'Player ID'): 'Player ID',
|
| 616 |
('Player Info', 'Batter'): 'Batter',
|
|
@@ -638,14 +641,32 @@ def server(input, output, session):
|
|
| 638 |
('Pitcher', 'RHP'): 'RHP'
|
| 639 |
}
|
| 640 |
|
| 641 |
-
# Convert to pandas
|
| 642 |
df_pandas = df_test_merge.to_pandas()
|
| 643 |
|
| 644 |
-
# Replace
|
| 645 |
df_pandas = df_pandas.replace({0: ''})
|
| 646 |
|
| 647 |
-
#
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
df_pivot_style = df_pandas.style
|
| 651 |
thick_border_cols = [3, 4, 13,22] # 0-based index
|
|
|
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
+
# List of desired columns, including RHP and LHP
|
| 602 |
desired_columns = [
|
| 603 |
'Player ID', 'Batter', 'Bats', 'GP', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
| 604 |
'C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'DH', 'LHP', 'RHP'
|
| 605 |
]
|
| 606 |
|
| 607 |
+
# Ensure all desired columns exist in the Polars DataFrame.
|
| 608 |
+
# If a column is missing, add it with a default value (e.g., empty string '')
|
| 609 |
+
for col in desired_columns:
|
| 610 |
+
if col not in df_test_merge.columns:
|
| 611 |
+
df_test_merge = df_test_merge.with_columns(pl.lit('').alias(col))
|
| 612 |
|
| 613 |
+
# Now select the columns in the desired order
|
| 614 |
+
df_test_merge = df_test_merge.select(desired_columns)
|
| 615 |
|
| 616 |
+
# Define your mapping for hierarchical columns.
|
| 617 |
cols = {
|
| 618 |
('Player Info', 'Player ID'): 'Player ID',
|
| 619 |
('Player Info', 'Batter'): 'Batter',
|
|
|
|
| 641 |
('Pitcher', 'RHP'): 'RHP'
|
| 642 |
}
|
| 643 |
|
| 644 |
+
# Convert the Polars DataFrame to a pandas DataFrame
|
| 645 |
df_pandas = df_test_merge.to_pandas()
|
| 646 |
|
| 647 |
+
# Replace any zeros with empty strings (if needed)
|
| 648 |
df_pandas = df_pandas.replace({0: ''})
|
| 649 |
|
| 650 |
+
# Create the MultiIndex for columns.
|
| 651 |
+
# The order of tuples should correspond exactly to the order in desired_columns.
|
| 652 |
+
# We can build the new MultiIndex by mapping the desired_columns using the `cols` dictionary.
|
| 653 |
+
new_columns = []
|
| 654 |
+
for col in desired_columns:
|
| 655 |
+
# Find the key in cols where value matches col.
|
| 656 |
+
# This assumes your mapping has unique values.
|
| 657 |
+
for key, value in cols.items():
|
| 658 |
+
if value == col:
|
| 659 |
+
new_columns.append(key)
|
| 660 |
+
break
|
| 661 |
+
|
| 662 |
+
# Double-check that the length of new_columns matches the DataFrame columns.
|
| 663 |
+
if len(new_columns) != len(df_pandas.columns):
|
| 664 |
+
raise ValueError("Mismatch in number of columns between new MultiIndex and DataFrame columns.")
|
| 665 |
+
|
| 666 |
+
# Apply the new MultiIndex to the DataFrame
|
| 667 |
+
df_pandas.columns = pd.MultiIndex.from_tuples(new_columns)
|
| 668 |
+
|
| 669 |
+
# Now your DataFrame has hierarchical columns and guaranteed 'LHP' and 'RHP' columns.
|
| 670 |
|
| 671 |
df_pivot_style = df_pandas.style
|
| 672 |
thick_border_cols = [3, 4, 13,22] # 0-based index
|