How to style boolean values by different colors in Pandas
In this article, you can find two approaches to color boolean columns in a Pandas DataFrame. The first one will change the background color while the second one will change the font color.
Step 1: Create Pandas DataFrame with boolean values
To start, let's create a DataFrame with several boolean columns which we will color.
For this article we are going to read data from Kaggle and search for a list of keywords - more info here: How to Add New Column Based on List of Keywords in Pandas DataFrame
You can download the data and read it manually by:
import pandas as pd
df = pd.read_csv('../data/medium_data.csv.zip')
in order to create multiple boolean columns we will search for list of words in a given column:
keywords=['how', 'data', 'question', 'guide']
df['keyword'] = df['title'].str.findall('|'.join(keywords)).apply(set).str.join(', ')
df[df['keyword'].str.len() > 5][['url', 'title', 'keyword']]
So this will be our starting data:
title | keyword | how | guide | data | question |
---|---|---|---|---|---|
Training on batch: how to split data effectively? | how, data | True | False | True | False |
Design & Data: how to humanize data | how, data | True | False | True | False |
Fyre Festival achieved perfect product-market fit (and that’s why we should question the Lean Startup and VC dogma) | question | False | False | False | True |
Using a ‘sneak attack’ question during your Designer interviews | question | False | False | False | True |
A question you may never have asked — culture fit or culture add? | question | False | False | False | True |
Step 2: Color background color of boolean column in Pandas
First we will demonstrate how to change the background color in a single or multiple boolean columns in Pandas DataFrame. For this purpose we will define a function which will add red background for True
and green for False
:
def color_boolean(val):
color =''
if val == True:
color = 'red'
elif val == False:
color = 'green'
return 'background-color: %s' % color
The function can be used for a single or several columns like this:
df.style.applymap(color_boolean, subset=['data'])
and for all boolean columns in a DataFrame like this:
df.style.applymap(color_boolean)
The result is:
Step 3: Change font color of boolean values in Pandas
Next if you like to highlight the True
and False
values of Pandas DataFrame then you can change only the font color. Let's define a different function:
def color_boolean(val):
color =''
if val == True:
color = 'red'
elif val == False:
color = 'green'
return 'color: %s' % color
It can be used in the same way only the result is different:
df.style.applymap(color_boolean)
Step 4: Use heatmap to color boolean values in Pandas
The last option which will be demonstrated for styling boolean columns in Pandas is with heatmap technique.
First we may need to prepare our data in order to be correctly styled - convert all boolean columns to int
- True
will become 1 and False
will become 0.
After that we can create a simple heatmap by:
df_temp[['how', 'guide', 'data', 'question']] = df_temp[['how', 'guide', 'data', 'question']].astype(int)
df_temp.style.background_gradient(cmap='Blues')
And the result will be: