Pandas Quick Read

How to apply conditional formatting to a DataFrame?

This post becomes the first-ever quick read tip of the new upcoming section on my blog. My goal of posting quick reads is to provide an easy, simple, uncomplicated, and clear understanding of some, of course, data science-related topics.

Styling DataFrames

Sometimes it’s required to present data in text tables, and highlight some values, control the precision of floats or simply show data in the format that is most convenient for you. Reading this post, you will find some ways to style your DataFrames. Let’s start!

Loading a tiny dataset

import pandas as pd
clicks = pd.read_csv('clicks.csv')
def highlight_max(s):
    highlight max number of variable
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

def color_negative_red(val):
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, blue for 2019 or 2020 values,
    black otherwise.
    color = 'red' if val < 0 else 'black'
    if val < 0:
        color = 'red'
    elif val == 2019 or val == 2020:
        color = 'blue'
        color = 'black'
    return 'color: %s' % color

Coloring negative numbers in red and years in blue

Highlighting max number in all columns

Highlighting max number in specific columns, subset=['clicks', 'roas'])

Highlighting max number in specific columns of a subset of original data frame

print('Maximum Average Numbers per Year and Quarter')

max_variables = clicks[['date_quarter','date_year','clicks',

# Highlighting max number in all columns

Formatting a specific column{'cpc':'{:.2%}'})

Coloring null values'red')

Different format to specific columns and using lambda{'cpc':'{:.2%}', 
                  'cost': lambda x:"{:.2f}".format(abs(x))})


So far, you have seen how to style your text tables using functions listed below. If you want to learn more about the Style object, you can take a look at the documentation.

  • style.applymap()
  • style.format()
  • style.apply()
  • style.highlight_null()
  • and two user functions

Visit my GitHub repo to access to this Jupyter Notebook.