> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to style boolean values by different colors in Pandas
- URL: https://datascientyst.com/style-boolean-values-different-colors-in-pandas/
- Published: 2021-08-14T08:58:35.000Z
- Updated: 2021-08-14T08:58:35.000Z
- Author: John D K
- Tags: Table

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](https://datascientyst.com/add-new-column-list-keywords-pandas-dataframe/)

You can download the data and read it manually by:

```python
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:

```python
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`:

```python
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:

```python
df.style.applymap(color_boolean, subset=['data'])

```

and for **all boolean columns in a DataFrame** like this:

```python
df.style.applymap(color_boolean)

```

The result is:

![style-boolean-values-different-colors-in-pandas](https://datascientyst.com/content/images/2021/08/style-boolean-values-different-colors-in-pandas.png)

## 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:

```python
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:

```python
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:

```python
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:

![style-boolean-values-different-colors-in-pandas-heatmap](https://datascientyst.com/content/images/2021/08/style-boolean-values-different-colors-in-pandas-heatmap.png)

## Notebook

- [Style Boolean Values by Different Colors in Pandas](https://github.com/softhints/datascientyst/blob/master/styling/2.style-boolean-values-different-colors-in-pandas.ipynb?ref=datascientyst.com)