[Python] How to change background color of a pandas column according to a value

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    How do I change the color of the Age column if the condition that the value is equal to 33 is met?

    My code:

    import pandas as pd

    df = pd.DataFrame.from_dict(
    {
    "Nombre": ["Mike", "Jordan", "John"],
    "Age": [33, 45, 20],
    "Lugar": ["Arg", "Pol", "Ind"]
    }
    )
    def _color_red_or_green(val):
    color = 'red' if val != 33 else 'green'
    return 'color: %s' % color

    df.style.applymap(_color_red_or_green)

    print(df)

    Continue reading...

Compartilhe esta Página