Highlight Pandas DataFrame's specific columns using applymap()


While presenting or explaining some facts and figures using Pandas DataFrame, we might need to highlight important rows and columns of the given data that help in making them more appealing, easier to understand and visually stunning. One way of highlighting the Pandas DataFrame's specific columns is by using the built-in method applymap().

Python Program to Highlight Pandas DataFrame using applymap()

To understand the code properly, we need to discuss the basics of Pandas and applymap():

Pandas

It is an open-source Python library that is mainly used for data analysis and manipulation. It can handle both relational and labeled data by performing various operations on specified data, such as cleaning, filtering, grouping, aggregating and merging.

applymap() method

It is used to apply a user-defined method element-wise to the entire Pandas DataFrame. To highlight a specific column, we first need to define a custom method that defines the required conditions for highlighting the columns and then, we use the applymap() method along with the style module to perform the operation.

Syntax

style.applymap(nameOfMethod)

Example 1

The following example demonstrates how to highlight specific columns in a Pandas DataFrame using the applymap() method.

Approach

  • First, import the Pandas library so that we can work with DataFrames.

  • Create a user-defined method that takes 'value' as a parameter and returns the background color. This method will check whether the value is divisible by 2 or not. If so, it will set the color to blue and if the value is divisible by 3, it sets the color to orange otherwise, the color will remain empty.

  • Create a DataFrame that contains a single column named 'Number' with some values.

  • Then, call the 'highlight_columns()' method with 'applymap()' to highlight the specified column and then display the highlighted columns.

import pandas as pd
# a user-defined method to highlight the data
def highlight_columns(value):
   color = ''
   if value % 2 == 0:
      color = 'background-color: blue'
   elif value % 3 == 0:
      color = 'background-color: orange'
   return color
# Creating a sample DataFrame using pandas
data = {'Number': [12, 15, 18, 21, 24]}
df = pd.DataFrame(data)
# Uisng highlight_columns() method to the 'Number' column
highlighted_df = df.style.applymap(highlight_columns)
# to show the columns
display(highlighted_df)

Output

Example 2

In the following example, we will highlight specific data of the columns based on the given condition.

Approach

  • Create a user-defined method that takes 'value' as a parameter and returns the background color. This method will check whether the value is greater than 50 or not. If it is, the color will be set to black otherwise, the color will remain empty.

  • Create a dictionary named 'data' with three keys: 'Name', 'Age' and 'Score'. Each key has a list as its value associated with it.

  • Then, call the 'highlight_columns()' method with 'applymap()' to highlight the specified column and then display the highlighted data of the columns using a built-in method named 'display()'. This method will be applied to only 'Age' and 'Score' columns of the DataFrame.

import pandas as pd
# a user-defined method to highlight the data
def highlight_columns(value):
   color = 'background-color: black' if value > 50 else ''
   return color
# Creating a sample DataFrame using pandas
data = {'Name': ['Ram', 'Shyam', 'Shrey'],
      'Age': [35, 42, 28],
      'Score': [75, 60, 90] }
df = pd.DataFrame(data)
# Uisng highlight_columns() method to the 'Age' and 'Score' columns
highlighted_df = df[['Age', 'Score']].style.applymap(highlight_columns)
# to show the columns
display(highlighted_df)

Output

Example 3

In the following example, we will highlight all three columns using applymap() method. To do so, we just need to set the 'color' variable to 'black' without specifying any condition as we did in our previous examples.

import pandas as pd
# a user-defined method to highlight the data
def highlight_columns(value):
   color = 'background-color: black'
   return color
# Creating a sample DataFrame using pandas
data = {'Name': ['Ram', 'Shyam', 'Shrey'],
      'Age': [35, 42, 28],
      'Score': [75, 60, 90]}
df = pd.DataFrame(data)
# Uisng highlight_columns() method to the 'Age' and 'Score' columns
highlighted_df = df[['Name', 'Age', 'Score']].style.applymap(highlight_columns)
# to show the columns
display(highlighted_df)

Output

Conclusion

We started this article by introducing the problem statement which is to highlight the specific column of Pandas Dataframe. Then, we discussed a possible solution by making use of the built-in method applymap() and also provided three different examples to understand its use.

Updated on: 21-Jul-2023

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements