Search code examples
pythonpandasdataframedata-analysis

How do I color flagged rows in an excel file exported from a pandas dataframe?


I am cleaning some data on an excel sheet using pandas dataframes, and I have a list of flagged indices. Is there a way to color these specific rows red or yellow in pandas itself before exporting?

The flagged indices are is a list: [30556, 30981, 55893... etc]

I'm not sure what to try here as I'm new to Data Analysis with Python, only done a beginners course on EdX, so any help would be appreciated :)


Solution

  • The word you wanted to Google for was "styling". Copious documentation is available.

    https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

    Briefly excerpting from that, one can define a styler

    def make_pretty(styler):
        styler.background_gradient(axis=None, vmin=1, vmax=5, cmap="YlGnBu")
        return styler
    

    and use it to e.g. map increasing values to background colors that are increasingly blue. Many additional styling methods are provided.

    weather_df.loc[start:end].style.pipe(make_pretty)
    

    rain values