Prettifying pandas DataFrames
By: Zolzaya Luvsandorj
Tags: Editors Pick, Jupyter Notebook, Getting Started, Pandas Dataframe, Python

1.4. Custom colour-code 🎨

In this last section, we will look at a few other ways to colour-code DataFrames using custom functions. We will use the following two methods to apply our custom styling functions:

◼️ .applymap() : elementwise

◼️ .apply() : column/row/tablewise

Elementwise application: .applymap()

Let’s create a small numerical data by slicing the top 8 rows from the numerical columns. We will use a lambda function to colour values above 190 as blue and the rest as grey:

df_num = df.select_dtypes('number').head(8)

(df_num.style

.applymap(lambda x: f"color: {'blue' if x>190 else 'grey'}"))

Let’s look at another example:

green = 'background-color: lightgreen'

pink = 'background-color: pink; color: white'

(df_num.style

.applymap(lambda value: green if value>190 else pink))

We can convert the lambda function into a regular function and pass it to .applymap() :

def highlight_190(value):

green = 'background-color: lightgreen'

pink = 'background-color: pink; color: white'

return green if value > 190 else pink df_num.style.applymap(highlight_190)

Row/Column/Tablewise application: .apply()

Let’s see how we could do the same formatting using .apply() :

def highlight_190(series):

green = 'background-color: lightgreen'

pink = 'background-color: pink; color: white'

return [green if value > 190 else pink for value in series] df_num.style.apply(highlight_190)

We can also chain them just like the previous functions:

(df_num.style

.apply(highlight_190)

.applymap(lambda value: 'opacity: 40%' if value<30

else None))

It’s useful to know how to use both .apply() and .applymap() . Here’s an example where we can use .apply() but not .applymap() :

def highlight_above_median(series):

is_above = series>series.median()

above = 'background-color: lightgreen'

below = 'background-color: grey; color: white'

return [above if value else below for value in is_above]

df_num.style.apply(highlight_above_median)

We find the median value by each column and highlight values higher than median in green and the rest in grey. We can also style the entire column based on conditions with .apply() :

def highlight(data):

n = len(data)

if data['sex']=='Male':

return n*['background-color: lightblue']

if data['sex']=='Female':

return n*['background-color: lightpink']

else:

return n*[''] df.head(6).style.apply(highlight, axis=1).hide_index()

Here, we have hidden DataFrame’s indices with .hide_index() for a cleaner look. If needed, you can also hide columns with .hide_columns() as well.

Lastly, most of these functions we looked at in this post take optional arguments to customise styling. The following two arguments are common and quite useful to know:

◼ ️ axis for along which axis to operate: columns, rows or the entire table

◼️ subset to select a subset of columns to style.

Photo by Lucas Benjamin on Unsplash

Hope you enjoyed learning about useful ways to prettify your DataFrames by colour-coding it. Styled DataFrames can help explore and analyse the data more easily and make your analysis more interpretable and attractive. If you are keen to learn more about styling, check out this useful documentation by pandas.