How change color of a specific columns on my Pandas dataframe?

Hello, new to Python and Streamlit, sorry for my code format

##pip install prettytable
import os
import requests
import streamlit as st
from bs4 import BeautifulSoup
from prettytable import PrettyTable
import pandas as pd
from decimal import Decimal


st.title(":blue[LI]:red[DOM] Temporada 2024-2025 :flag-do::baseball:")
st.header("Tabla de Posiciones")

url = '#'
url2 = '#
respuesta = requests.get(url)
respuesta2 = requests.get(url2)

columns_standing = ["Equipo", "G - P", "PCT", "JD","Casa", "Ruta", "Racha"]
columns_offensive_stats = ["Equipo","K%", "BB%"]
rows_standing = []
rows_offensive_stats = []
total_teams = 6

##aqui ponemos las estadisticas basicas
if respuesta.status_code == 200:
  soup = BeautifulSoup(respuesta.text, 'html.parser')
  soup2 = BeautifulSoup(respuesta2.text, 'html.parser')

  for x in range(total_teams):
    x += 1
    team_name = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[0].find_all('a')[0].get_text().strip()
    wins_loses = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[2].get_text().strip() + " - " + soup.find_all('table')[1].find_all('tr')[x].find_all('td')[3].get_text().strip()
    pct = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[4].get_text().strip()
    gb = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[5].get_text().strip()
    casa = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[6].get_text().strip()
    ruta = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[7].get_text().strip()
    racha = soup.find_all('table')[1].find_all('tr')[x].find_all('td')[8].get_text().strip()

    ##segundo
    ab = int(soup2.find_all('table')[1].find_all('tr')[x].find_all('td')[2].get_text().strip())

    k = int(soup2.find_all('table')[1].find_all('tr')[x].find_all('td')[11].get_text().strip())
    bb = int(soup2.find_all('table')[1].find_all('tr')[x].find_all('td')[9].get_text().strip())
    pa = ab + bb

    k_rate = ((k/pa)*100)
    bb_rate = ((bb/pa)*100)


    k_rate = (format(k_rate,'.1f'))+"%"
    bb_rate = (format(bb_rate,'.1f'))+"%"

    rows_standing.append([team_name, wins_loses, pct, gb, casa, ruta, racha])
    rows_offensive_stats.append([team_name,k_rate,bb_rate])

df_standing = pd.DataFrame(rows_standing, columns=columns_standing)
df_standing.set_index("Equipo", inplace=True)

df_offensive_stats = pd.DataFrame(rows_offensive_stats, columns=columns_offensive_stats)
df_offensive_stats.set_index("Equipo", inplace=True)

df2 = df_standing.style.set_table_styles({
    'Equipo': [{'selector': '',
           'props': [('color','blue')]}],
},overwrite=False)

st.table(df2)
st.header("Offensive Stats")
st.table(df_offensive_stats)

So basically I want to change color column Equipo to blue, but it doesn’t.

Use st.dataframe

You need to create a styled data frame and pass it to st.dataframe:

Demo

import streamlit as st
import pandas as pd
import numpy as np

# Create mock data
np.random.seed(0)

df = pd.DataFrame({
    'A': np.random.randn(3),
    'B': np.random.randn(3),
    'C': np.random.randn(3)
})

st.title('DataFrame Column Highlighting')

# Display original dataframe
st.subheader('Original DataFrame')
st.dataframe(df)

# Select column to highlight
selected_column = st.selectbox('Select a column to highlight', options=df.columns)

# Create a styled data frame
df_styled = df.style.set_properties(subset=[selected_column], **{'background-color': 'yellow'})

# Display the styled dataframe
st.subheader('Styled DataFrame')
st.dataframe(df_styled)

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.