Highlight the maximum value in last two columns in Pandas - Python Last Updated : 26 Jul, 2020 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to highlight the maximum values in Pandas Dataframe. Let's first make a dataframe: Example: Python3 # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sumit Tyagi', 'Sukritin', 'Akriti Goel', 'Sanskriti', 'Abhishek Jain'], 'Age': [22, 20, np.nan, np.nan, 22], 'Marks': [90, 84, 33, 87, 82]} # Converting Dictionary to Pandas Dataframe df = pd.DataFrame(dict) # Print Dataframe df Output: Now, come to the highlighting part. Our objective is to highlight cells with maximum values in last 2 columns. Method 1: Highlighting Cell with maximum value in last 2 columns We will do this by using the highlight_max() method of DataFrame property. highlight_max() method takes 3 arguments, subset: name of the columns of which you want to find the maximumcolor: name of the color with which you want to highlight the cellaxis: (0/1) based on which axis you want to find the maximum. Example: Python3 # Highlighting the maximum values # of last 2 columns df.style.highlight_max(subset = ['Age', 'Marks'], color = 'lightgreen', axis = 0) Output: Method 2: Instead of using column names we generalise it to last two columns Example: Python3 # Highlighting the maximum values of # last 2 columns df.style.highlight_max(subset = df.columns[-2:], color = 'lightgreen', axis = 0) Output: Method 3: Highlighting the text instead of cellExample: Python3 # Defining custom function which returns # the list for df.style.apply() method def highlight_max(s): is_max = s == s.max() return ['color: green' if cell else '' for cell in is_max] df.style.apply(highlight_max, subset = df.columns[-2:]) Output: Method 4: Highlighting cell with maximum valuesExample: Python3 # Defining custom function which returns # the list for df.style.apply() method def highlight_max(s): is_max = s == s.max() return ['background: lightgreen' if cell else '' for cell in is_max] df.style.apply(highlight_max, subset = df.columns[-2:]) Output: Create Quiz Comment S sumit_tyagi Follow 0 Improve S sumit_tyagi Follow 0 Improve Article Tags : Python Python-pandas Python pandas-dataFrame pandas-dataframe-program Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like