Let us see how to highlight elements and specific columns of a Pandas DataFrame. We can do this using the applymap() function of the Styler class.
Styler.applymap()
Syntax : Styler.applymap(self, func, subset = None, **kwargs)
Parameters :
- func : takes a scalar and returns a scalar.
- subset : valid indexer to limit data to before applying the function.
- **kwargs : dict pass along to func.
Returns : Styler
Let’s understand with examples:
First of all create a simple data frame:
# importing pandas as pd import pandas as pd # creating the dataframe df = pd.DataFrame({"A" : [14, 4, 5, 4, 1], "B" : [5, 2, 54, 3, 2], "C" : [20, 20, 7, 3, 8], "D" : [14, 3, 6, 2, 6]}) print("Original DataFrame :") display(df) |
Output :

Example 1 : For every cell in the DataFrame, if the value is less than 6 then we will highlight the cell with red color, otherwise with blue color.
# function definition def highlight_cols(s): color = 'red' if s < 6 else 'blue' return 'background-color: % s' % color # highlighting the cells display(df.style.applymap(highlight_cols)) |
Output :

Example 2 : This time we will highlight only the cells in some specified columns.
# function definition def highlight_cols(s): return 'background-color: % s' % 'yellow' # highlighting the cells display(df.style.applymap(highlight_cols, subset = pd.IndexSlice[:, ['B', 'C']])) |
Output :

Highlight specific columns with the help of Indexing:
Python3
df.style.applymap(highlight_cols, subset = pd.IndexSlice[:, ['B', 'C']]) |

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Highlight Pandas DataFrame's specific columns using apply()
- Highlight the maximum value in last two columns in Pandas - Python
- Python | Pandas dataframe.applymap()
- Difference between map, applymap and apply methods in Pandas
- Reading specific columns of a CSV file using Pandas
- Highlight the nan values in Pandas Dataframe
- Highlight the maximum value in each column in Pandas
- Highlight the negative values red and positive values black in Pandas Dataframe
- Highlight the minimum value in each column In Pandas
- Highlight a Bar in Bar Chart using Altair in Python
- How to import excel file and find a specific column using Pandas?
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Split a String into columns using regex in pandas DataFrame
- Using dictionary to remap values in Pandas DataFrame columns
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Return multiple columns using Pandas apply() method
- Insert a given column at a specific position in a Pandas DataFrame
- Rename specific column(s) in Pandas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

