Add colored indicators to your dataframes html representation

Mike Driscoll recently tweeted about making colored out with pandas DataFrames and I just had to try it for myself

Use Case

First though... why? My biggest use case is a monitoring pipeline of mine... The details aside, the output of my pipeline is a dataframe where each row has information about a failed pipeline that I need to go look into. I dump that result to a simle html file that's hosted on an internal site and the file is updated every couple of hours. Adding some colored indicators automatically to the rows to help me assess severity of each record would be a handy way to quickly get an understanding the state of our pipelines.

How?

The docs for the applymap method state simply:

Apply a CSS-styling function elementwise.

Updates the HTML representation with the result.

So we can write a function that returns color: {color} based on the dataframe values and when we drop that dataframe to html we'll have some simple css styling applied automagically!

By default the function will be applied to all columns of the dataframe, but that's not useful if the columns are different types which is usually the case. Luckily there is a subset keyword to only apply to the columns you need!

Consider my example

sandbox   main via 3.8.11(sandbox) ipython
 df = pd.read_csv("cars.csv")

sandbox   main via 3.8.11(sandbox) ipython
 def mpg_color(val: float):
...:     color = "red" if val < 21 else "green"
...:     return f"color: {color}"

sandbox   main via 3.8.11(sandbox) ipython
 df.style.applymap(mpg_color, subset="mpg").to_html("color.html")

I want to quickly see if the mpg is any good for the cars in the cars dataset and I'll define "good" as better than 21 mpg (not great I know but just for the sake of discussion...)

The function returns an appropriate css string and after I style.applymap on just the mpg column we get this!

  Unnamed: 0 mpg cyl disp hp drat wt qsec vs am gear carb
0 Mazda RX4 21.000000 6 160.000000 110 3.900000 2.620000 16.460000 0 1 4 4
1 Mazda RX4 Wag 21.000000 6 160.000000 110 3.900000 2.875000 17.020000 0 1 4 4
2 Datsun 710 22.800000 4 108.000000 93 3.850000 2.320000 18.610000 1 1 4 1
3 Hornet 4 Drive 21.400000 6 258.000000 110 3.080000 3.215000 19.440000 1 0 3 1
4 Hornet Sportabout 18.700000 8 360.000000 175 3.150000 3.440000 17.020000 0 0 3 2