I got this original table with the styles. The colors of each cell is the position of the value in a percentile scale from percentile 1 (blue) to percentile 100 (red)
For example, the first row (Cristhian Adames) is percentile 100 (red) on Avg. Exit Velocity, let’s look on him on the multiselect:
Obviusly this a normal behavior. But how can I maintain the original color for each cell no matter what you are looking for?
This is my code (hope this code fragment is enough):
show_data()
is a function that return the original dataframe and save to lidom_statcast
lidom_statcast = show_data()
min_pa = [5, 10, 20, 30, 40, 50, 75, 100]
min_bbe = [1, 5, 10, 15, 20, 25, 30, 40]
players_list = lidom_statcast['Nombre'].to_list()
l2 = []
l2 = players_list
l2.append('Todos')
col1, col2, col3 = st.columns([1,1,5])
with col1:
option_pa = st.selectbox(
"Min. PA",
(min_pa),
)
with col2:
option_bbe = st.selectbox(
"Min. BBE",
(min_bbe),
)
with col3:
players_dropdown = st.multiselect('Jugadores', l2, default="Todos", placeholder="Seleccione los jugadores o Todos")
if 'Todos' in players_dropdown:
players_dropdown = players_list
lidom_statcast = lidom_statcast.loc[(lidom_statcast['PA'] >= option_pa) & (lidom_statcast['BBE'] >= option_bbe) & (lidom_statcast['Nombre'].isin(players_dropdown))]
mapper = {'aEV_percentile': '{0:.0f}',
'EV': '{0:.1f}',
'LA': '{0:.0f}',
'barrels': '{0:.0f}',
'Brl%': '{0:.1f}',
'brl_percentile': '{0:.0f}',
'hhb': '{0:.0f}',
'HardHit%': '{0:.1f}',
'hh_percentile': '{0:.0f}',
'SwSp%': '{0:.1f}',
'swsp_percentile': '{0:.0f}',
'Whiff%': '{0:.1f}',
'whiff_percentile': '{0:.0f}',
'K%': '{0:.1f}',
'k_rate_percentile': '{0:.0f}',
'BB%': '{0:.1f}',
'bb_rate_percentile': '{0:.0f}',
'Chase%': '{0:.1f}',
'o_chase_ratepercentile': '{0:.0f}'}
gradient = matplotlib.colors.LinearSegmentedColormap.from_list("", ['blue', 'white', '#f81707'])
gradient_revert = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#f81707', 'white', 'blue'])
data_style = lidom_statcast.style.format(mapper).background_gradient(subset=['EV'], cmap=gradient,vmin=lidom_statcast.EV.min(),vmax=lidom_statcast.EV.max())
data_style = data_style.background_gradient(subset=['Brl%'], cmap=gradient,vmin=lidom_statcast['Brl%'].min(),vmax=lidom_statcast['Brl%'].max())
data_style = data_style.background_gradient(subset=['HardHit%'], cmap=gradient,vmin=lidom_statcast['HardHit%'].min(),vmax=lidom_statcast['HardHit%'].max())
data_style = data_style.background_gradient(subset=['SwSp%'], cmap=gradient,vmin=lidom_statcast['SwSp%'].min(),vmax=lidom_statcast['SwSp%'].max())
data_style = data_style.background_gradient(subset=['Whiff%'], cmap=gradient_revert,vmin=lidom_statcast['Whiff%'].min(),vmax=lidom_statcast['Whiff%'].max())
data_style = data_style.background_gradient(subset=['K%'], cmap=gradient_revert,vmin=lidom_statcast['K%'].min(),vmax=lidom_statcast['K%'].max())
data_style = data_style.background_gradient(subset=['BB%'], cmap=gradient,vmin=lidom_statcast['BB%'].min(),vmax=lidom_statcast['BB%'].max())
data_style = data_style.background_gradient(subset=['Chase%'], cmap=gradient_revert,vmin=lidom_statcast['Chase%'].min(),vmax=lidom_statcast['Chase%'].max())
data_style = data_style.hide(axis=0)
st.dataframe(data_style,
column_config={
"picture": st.column_config.ImageColumn(
" ", help=None, width="small"
),
"BBE": st.column_config.Column(
"BBE", help=None, width="small"
),
"SwSp%": st.column_config.Column(
"LA SwSp%", help=None, width="small"
),
"EV": st.column_config.Column(
"Avg. Exit Velocity", help=None, width="small"
),
"Brl%": st.column_config.Column(
"Barrel %", help=None, width="small"
),
},
width=2000,
height=1000,
hide_index=True)
Sorry if the code is messy! I am new to Python and Streamlit and this is one of my first project.