pythonpandasdataframepandas-styles

Style dataframe adding emoji/icon next to number if it's negative or positive


Context: I'm trying to stylize dataframe cells based on their percentage values and while I'm able to show gradients for each cell depending on the values, is it possible to add emojis/icons next to it similar to excel formatting?

Desired output:

Arrow icons

Attempts:

I'm just using here a gradient for the cells

cmap = LinearSegmentedColormap.from_list(
        name='test', 
        colors=['#FF3409','#F28068','#FFFFFF','#ADF2C7','#4DCA7C']
)
(df.style
   .background_gradient(
                subset=['Percentage'],
                cmap=cmap,
                axis=None)\
   .format(
                {
                    "Percentage":"{:.1%}",
                }
                )
            )
       

How could I add those arrows as well alongide the color gradient?


Solution

  • This worked for me:

       (df.style
            .format('{:.0%}')
            .background_gradient(
                cmap=cmap_red_green,
                axis=None
            )
            .format(lambda x :"⇧ {:.0%}".format(x) if x > 0 else "⇩ {:.0%}".format(x))
        )
    

    Just need to find other arrows to be more closely related with the arrows I've shown