Change background color based on value

hey I was wondering if there is a color change based on value. Say I have a csv file that has a column that says true or false can I make the cell that has true value have a green background and false in red.

Hey @yshvrdhn,

Are you referring to colouring your dataframe ? If yes then Streamlit accepts Styling, so here I will color survivors in green and deaths in red given value of the Survived column.

import pandas as pd
import streamlit as st 


df = pd.read_csv("data/titanic.csv")

def highlight_survived(s):
    return ['background-color: green']*len(s) if s.Survived else ['background-color: red']*len(s)

def color_survived(val):
    color = 'green' if val else 'red'
    return f'background-color: {color}'

st.dataframe(, axis=1))
st.dataframe(, subset=['Survived']))

Sorry for the flashy colors :sweat_smile:

1 Like