Is it possible to show color red for negative numbers and green for positive numbers?
import streamlit as st
import pandas as pd
df = pd.DataFrame({'a': ['A', 'B', 'C'],
'b': [-2, 1, 4]})
st.dataframe(df,
column_config={
'a': st.column_config.Column('Key'),
'b': st.column_config.Column('Value')
},
hide_index=True)
edsaac
2
Yes, that can be done using pandas.Styler functions.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.formats.style.Styler.applymap.html
For example:

import streamlit as st
import pandas as pd
def bgcolor_positive_or_negative(value):
bgcolor = "lightcoral" if value < 0 else "lightgreen"
return f"background-color: {bgcolor};"
df = pd.DataFrame({'a': ['A', 'B', 'C'],
'b': [-2, 1, 4]})
styled_df = df.style.applymap(bgcolor_positive_or_negative, subset=['b'])
st.dataframe(styled_df,
column_config={
'a': st.column_config.Column('Key'),
'b': st.column_config.Column('Value')
},
hide_index=True)
1 Like