Highlighting Cells in a Pandas DataFrame with Custom Styling

Highlighting Cells in a Pandas DataFrame

In this article, we’ll explore how to highlight all cells in a pandas DataFrame that contain a specific object. We’ll dive into the world of pandas styling and learn how to achieve this using a custom function.

Introduction to Pandas Styling

Pandas is a powerful library for data manipulation and analysis in Python. One of its key features is data visualization, which includes styling DataFrames. The style attribute allows you to apply various styles to your DataFrame, making it easier to visualize and interact with the data.

The style.applymap() function applies a given function element-wise to a DataFrame. This means that for each cell in the DataFrame, the function is applied to the value at that position.

Understanding the Problem

Let’s consider the example Dataframe provided:

LocationCountryGroup
1GermanyA
1ArmeniaC
1ZimbabweA
2IndiaB
2ZimbabweC

We want to highlight all cells that contain the string “Zimbabwe”.

Solving the Problem

To achieve this, we can define a custom function zimbabwe_red() that checks if the value in each cell is equal to “Zimbabwe”. If it is, the function returns a string indicating the color of the text. Otherwise, it returns an empty string.

def zimbabwe_red(val):
    """Return 'color: red' if val is 'Zimbabwe', otherwise 'black'."""
    color = 'red' if val == "Zimbabwe" else 'black'
    return f"color: {color}"

Applying the Style

Now that we have our custom function, we can apply it to the DataFrame using style.applymap().

import pandas as pd

# Create a sample Dataframe
df = pd.DataFrame({
    "Location": [1, 1, 1, 2, 2],
    "Country": ["Germany", "Armenia", "Zimbabwe", "India", "Zimbabwe"],
    "Group": ["A", "C", "A", "B", "C"]
})

# Define the custom function
def zimbabwe_red(val):
    color = 'red' if val == "Zimbabwe" else 'black'
    return f"color: {color}"

# Apply the style to the DataFrame
df.style.applymap(zimbabwe_red)

Understanding the Output

When we run this code, Pandas will apply our custom function element-wise to each cell in the DataFrame. The resulting output will be a styled DataFrame where all cells containing “Zimbabwe” are highlighted in red.

LocationCountryGroup
1GermanyA
1ArmeniaC
1ZimbabweA
2IndiaB
2ZimbabweC

As you can see, the cells containing “Zimbabwe” are now highlighted in red.

Additional Styles

Pandas also supports various other styles that you can apply to your DataFrame. These include:

  • background-color: sets the background color of a cell
  • font-size, font-weight, and font-style: set the font properties for a cell
  • border-width and border-style: set the border width and style for a cell

You can combine multiple styles to create a more complex appearance.

Customizing Your Style

While pandas styling provides a lot of flexibility, it can also be limited. If you need to customize your style further or achieve more complex visualizations, consider using other libraries such as Matplotlib or Seaborn.

However, for simple use cases like highlighting specific values in a DataFrame, the style.applymap() function is an efficient and convenient solution.

Conclusion

In this article, we explored how to highlight all cells in a pandas DataFrame that contain a specific object. We defined a custom function zimbabwe_red() and applied it to our Dataframe using style.applymap(). By understanding the basics of pandas styling and applying some creative thinking, you can achieve complex visualizations with ease.

References


Last modified on 2023-05-29