Help styling arrows in dataframe

Once you concat the dataframes, you need to re-apply the styling to columns that were previously styled. Extending my example from above:

import numpy as np
import pandas as pd

import streamlit as st


@st.cache_data
def load_data():
    # range of values from -100 to 100
    return pd.DataFrame(np.random.randint(-6, 6, size=(6, 2)), columns=list("ab"))


def _format_arrow(val):
    return f"{'↑' if val > 0 else '↓'} {abs(val):.0f}%" if val != 0 else f"{val:.0f}%"


def _color_arrow(val):
    return "color: green" if val > 0 else "color: red" if val < 0 else "color: black"

@st.cache_data
def second_df():
    df = pd.DataFrame(
        {
            "median_sale_price": 100000,
            "median_ppsf": 1000,
            "median_days_on_market": 30,
            "inventory": 100,
            "homes_above_list_price": 10,
            "homes_sold": 100,
        },
        index=[0],
    )
    df = df.T
    df[""] = df[0]
    df = df.drop(columns=[0])
    return df


col1, col2 = st.columns(2)

df = load_data()
col1.write("First dataframe")
col1.dataframe(df)
styled_df = df.style.format(_format_arrow).applymap(_color_arrow, subset=["a", "b"])
col2.write("Styled dataframe")
col2.dataframe(styled_df)

df_2 = second_df()
df_2 = df_2.reset_index()
col1.write("Second dataframe")
col1.dataframe(df_2)

# Concat the two dataframes df and df_2 into one dataframe
df_3 = pd.concat([df_2, df], axis=1)
col2.write("Concatenated dataframe")
col2.dataframe(df_3)

# Apply the styling to the concatenated dataframe
df_3 = df_3.style.format(_format_arrow, subset=["a", "b"]).applymap(
    _color_arrow, subset=["a", "b"]
)
st.write("Styled concatenated dataframe")
st.dataframe(df_3, use_container_width=True)

2 Likes