Styling Options for DataFrames

Learn about all the different ways to customize the formatting of datasets. Understand how to use the Styler class with Gradio.

Styling the DataFrames

In this lesson, we will explore how we can style the DataFrames to make them more appealing and fit for purpose. This includes formatting the values, highlighting certain cells, adding color, and marking important figures, just to name a few.

The pandas DataFrame has an attribute style that returns a Styler object. Suppose we have a DataFrame defined as df, then we can access this attribute as follows: df.style.

We can then use this accessor to help modify the Styler object and control how the DataFrame is displayed and formatted on the web. This styling is done through CSS. We write style "functions" that take scalars, DataFrame or Series, and it returns like-indexed DataFrame or Series with CSS ‘attribute: value’ pairs for the values. The Styler objects collect these styles before rendering.

We will now explore how we can use the Styler objects to format our DataFrames.

Methods for formatting the display

Styler has many built-in methods to apply formatting to DataFrames. We will go over some commonly used ones.

Format

The format method is used to format the text display value of cells and can be accessed by df.style.format(). It includes the following parameters to tailor how the DataFrame text is formatted:

  • formatter: It is an object that defines how values are displayed.

    • If None, then the default formatter will be used. For floats and complex numbers, this defaults to the pandas display precision (unless overridden by the precision parameter).

    • If provided as a string, this is assumed to be a valid Python format specification and is wrapped in a callable as string.format(x). For example, this can be '{:.2f}' which will format as two decimal places.

    • If provided as a dict, the keys should correspond to the column names, and values should be string or callable as above.

  • subset: This specifies which part of the DataFrame the formatter should be applied to. This can either be a 2D input that fits into the syntax DataFrame.loc[<subset>], or a 1D input or single key that fits into the syntax DataFrame.loc[:, <subset>] (e.g. [0,1] for the first two columns).

  • na_rep: It replaces missing values with the string specified.

  • precision: It is the floating point precision to use for display purposes (if not already specified in formatter).

One thing to note about Stylers is that they only affect the display of the DataFrame, so they must be used with something that displays it in the UI, e.g., in Gradio. Printing a DataFrame in the terminal won’t show the Styler changes. It also requires the DataFrame in Gradio to have interactive set to False, otherwise it won’t show the changes. By default, interactive is set to False, unless the DataFrame is used in some event.

Get hands-on with 1300+ tech skills courses.