Highlight Pandas DataFrame's Specific Columns using Apply()


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

Python Program to Highlight Pandas DataFrame using apply()

Before jumping to the example program directly, it is necessary to discuss the basics of Pandas and apply().

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.

apply() method

It is used to apply a user-defined method to each element of the 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 apply() method along with the style module to perform the operation.

Syntax

style.apply(nameOfMethod)

Example 1

The following example shows the practical implementation of apply() method.

Approach

  • The first step is to import the pandas library with reference name 'pd'.

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

  • Now, define a dataframe to represent key as column name and its value as the data for that column.

  • Define a user-defined method named 'highlight_columns' along with a parameter 'col'. This method will set and return the 'color' variable to 'skyblue' to the column name 'Age' and 'Score' and 'White' to 'Name'.

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

import pandas as pd
# defining a DataFrame
data = {
   'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
   'Age': [25, 30, 35, 40],
   'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# a user-defined method to highlight specific columns
def highlight_columns(col):
   color = 'skyblue' if col.name in ['Age', 'Score'] else 'white' 
   return ['background-color: {}'.format(color) for _ in col]
# calling method using apply()
styled_df = df.style.apply(highlight_columns)
# to show the highlighted column
styled_df

Output

Example 2

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

import pandas as pd
# defining a DataFrame
data = {
   'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
   'Age': [25, 30, 35, 40],
   'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# a user-defined method to highlight specific columns
def highlight_columns(col):
   color = 'blue' 
   return ['background-color: {}'.format(color) for _ in col]
# calling method using apply()
styled_df = df.style.apply(highlight_columns)
# to show the highlighted portion
styled_df

Output

Example 3

In this example, we will highlight only the specific data of given column based on defined condition.

Approach

  • Like the previous two examples, we will import the pandas library and create a dictionary.

  • Inside the 'highlight_columns()' method, define multiple conditions to highlight the column using elif statement.

  • If the column name is 'Age', we iterate over each value in the column and check if it is greater than 30. If it is, we return the background-color to blue.

  • If the column name is 'Score', we iterate over each value in the column and check if it is greater than or equal to 90. If it is, we return the background-color to green.

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

import pandas as pd
# defining a DataFrame
data = {
   'Name': ['Ram', 'Shyam', 'Mohan', 'Shrey'],
   'Age': [25, 30, 35, 40],
   'Score': [80, 90, 85, 95]
}
df = pd.DataFrame(data)
# method for highlighting specific columns
def highlight_columns(col):
   if col.name == 'Age':
      return ['background-color: blue' if val > 30 else '' for val in col]
   elif col.name == 'Score':
      return ['background-color: green' if val >= 90 else '' for val in col]
   else:
      return ['' for _ in col]
# calling the method using apply()
styled_df = df.style.apply(highlight_columns)
# to display the highlighted DataFrame
styled_df

Output

Conclusion

In this article, we have learned the use of apply() method in highlighting the data of specified columns. It is used with the style module of Pandas. We also discovered how to create a Pandas DataFrame.

Updated on: 21-Jul-2023

722 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements