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 

# https://stackoverflow.com/questions/43596579/how-to-use-python-pandas-stylers-for-coloring-an-entire-row-based-on-a-given-col

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(df.style.apply(highlight_survived, axis=1))
st.dataframe(df.style.applymap(color_survived, subset=['Survived']))

Sorry for the flashy colors :sweat_smile:

9 Likes

@andfanilo Is there a way to do this with background gradients?

1 Like

yes, here has a way, you can replace “background-color:green” with “background-image: linear-gradient(to right, yellow , green);”

2 Likes

How can i achieve this in a table ?

1 Like

You can achieve this in similar way as st.dataframe. Below is a snippet of the code.

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

st.table(df[['Tweet_Date', 'Text', 'Sentiment']].sort_values(['Tweet_Date'], ascending=False).
               reset_index(drop=True).head(20).style.applymap(color_survived, subset=['Sentiment']))
3 Likes

Is there a table equivalent here for entire row coloring as well? I’ve been trying to replicate the dataframe approach for that on a table here without success.

What if I don’t want to change the color on the “Survived” column. I only want to change the highlight color on “Name”? The color style still need to be applied based on the value in “Survived” column?

Hi,

I have used the following code

def cooling_highlight(val):
    color = '#ACE5EE' if val else 'white'
    return f'background-color: {color}'

and applied it to my table :

st.dataframe(df1
                     .style.applymap(cooling_highlight, subset=['Cooling inputs', 'Cooling outputs'])
                    #  ,(heating_highlight, subset=['Heating inputs', 'Heating outputs'])             
                     ,height=530
                     )

It works as expected however the values in my df1 were rounded (2 decimals), when applying the color background, all of my values in my table (not just the selected columns) have many decimals again. Has anybody encountered this before?

Also, I have tried adding a different style to other columns but it does not seem to accept it. Is there a way to apply different styles to different columns?

Could You Solve the same. I have got the same problem?

Hi AndFanilo,

I only want to apply the colour scheme on columns only ending with “_CAUTION”. I have used this functionality for the code

caution_ls=final_df2.filter(like=‘_CAUTION’).columns

st.dataframe(final_df2.style.applymap(color_survived, subset=[caution_ls]))

It is throwing this error:

‘Styler’ object has no attribute ‘style’

Hello! I want to add a problem I met: I want to color the cell if the value is ‘Change’ to yellow, and the rest as white, but the dashboard shows many weird black cell colors.

st.dataframe(
    input_change_ptp.style.applymap(
        color_coding_change_flag_2, subset=['If Input Change']
    ),
    height=600,
    hide_index=True
)  # output component
def color_coding_change_flag_2(val):
    color = "antiquewhite" if val != "-" else "snow1"
    return f'background-color: {color}'

and the dashboard displayed shows: