Let’s see various methods to Highlight the positive values red and negative values black in Pandas Dataframe.
First, Let’s make a Dataframe:
Python3
# Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = { 'Name' : [ 'Sukritin' , 'Sumit Tyagi' , 'Akriti Goel' , 'Sanskriti' , 'Abhishek Jain' ], 'Age' : [ 22 , 20 , 45 , 21 , 22 ], 'Marks' : [ 90 , 84 , - 33 , - 87 , 82 ] } # Converting Dictionary to # Pandas Dataframe df = pd.DataFrame( dict ) # Print Dataframe print (df) |
Output:
Now, come to the highlighting part. Our objective is to highlight negative values red and positive values black.
Method 1: Using Dataframe.style.apply().
Syntax: DataFrame.style.apply(self, func, axis=0, subset=None, **kwargs)
Parameters:
- func: It should take a pandas.Series or pandas.DataFrame based on the axis and should return an object with the same shape.
- axis: {0 or ‘index’, 1 or ‘columns’, None}, default 0. Apply to each column (axis=0 or ‘index’), to each row (axis=1 or ‘columns’), or to the entire DataFrame at once with axis=None.
- subset: Set of columns or rows on which you want to call the func.
- **kwargs: Pass along to func.
Returns: Styler object.
Example 1: Highlighting text.
Python3
# Define a function for colouring # negative values red and # positive values black def highlight_max(s): if s.dtype = = np. object : is_neg = [ False for _ in range (s.shape[ 0 ])] else : is_neg = s < 0 return [ 'color: red;' if cell else 'color:black' for cell in is_neg] # Using apply method of style # attribute of Pandas DataFrame df.style. apply (highlight_max) |
Output:
Example 2: Highlighting cells instead of text.
Python3
# Define a function which # returns the list for # df.style.apply() method def highlight_max(s): if s.dtype = = np. object : is_neg = [ False for _ in range (s.shape[ 0 ])] else : is_neg = s < 0 return [ 'background: red; color:white' if cell else 'background:black; color:white' for cell in is_neg] # Using apply method of style # attribute of Pandas DataFrame df.style. apply (highlight_max) |
Output:
Method 2: Using dataframe.style.applymap() method.
Syntax: DataFrame.style.applymap(self, func, subset=None, **kwargs)
Parameters:
- func: It takes a scalar value and return the scalar values
- subset: Set of columns or rows on which you want to call the func.
- **kwargs: Pass along to func.
Returns: Styler object.
Example 1: Highlighting text.
Python3
# Define a function for # colouring negative values # red and positive values black def highlight_max(cell): if type (cell) ! = str and cell < 0 : return 'color: red' else : return 'color: black' df.style.applymap(highlight_max) |
Output:
Example 2: Highlighting cells instead of text.
Python3
# Define a function which # returns string for # applymap() method def highlight_max(cell): if type (cell) ! = str and cell < 0 : return 'background: red; color:black' else : return 'background: black; color: white' df.style.applymap(highlight_max) |
Output:
Note: pandas.DataFrame.applymap() method passes only single cell into the callable function while the pandas.DataFrame.apply() passes the pandas.Series to the callable function.
Reference: Styling in Pandas