pythonpandasexceldataframepandas-styles

Styling negative numbers in pandas


I have a dataframe that I am exporting to Excel. I would also like to style it before the export.

I have this code which changes the background color and text color and works fine, but I would like to add to it:

df.style.set_properties(**{'background-color': 'black',
                           'color': 'lawngreen',
                           'border-color': 'white'}).to_excel(writer, sheet_name='Sheet1', startrow=rowPos, float_format = "%0.5f")

I need columns with strings and dates to have a white text color, and then positive numbers to be green and negative numbers to be red. I pulled these styles directly from pandas documentation on styling since I have never used it before, and am unsure how to achieve these results.

Lets say my dataframe looks like this:

StartDate   ExpiryDate  Commodity   Quantity  Price   Total
---------   ----------  ----------  -------   -----   -----
02/28/2024  12/28/2024  HO          10000     -3.89   -38900
02/28/2024  12/28/2024  WPI         10000      4.20    42000

how could I achieve what I am looking for?


Solution

  • I'd break it down into three steps (see the comments #) :

    st = (
        df.style
        # 1-applying the default styles
        .set_properties(**default_css)
        # 2-formatting the numeric columns
        .apply(
            lambda df_: df_.select_dtypes("number")
            .lt(0).replace({True: tc(neg), False: tc(pos)}),
            axis=None,
        )
        .format(precision=2) # this one is optional
        # 3-formatting the string-like dates and strings
        .map(lambda v: tc(obj) if isinstance(v, str) else "")
    )
    
    # st.to_excel("output.xlsx", index=False) # uncomment to make an Excel
    

    Output :

    enter image description here

    Used CSS :

    default_css = {
        "background-color": "black",
        "border": "1px solid white",
    }
    
    tc = "color: {}".format # css text color
    obj, pos, neg = "white", "lawngreen", "red"