0

Given the following DataFrame and list of cells to highlight:

import pandas as pd
import numpy as np

# Example DF
df = pd.DataFrame(
    np.random.randint(0, 100, size=(10, 10)),
    columns=list('ABCDEFGHIJ')
)

cells_to_highlight = [
    (1, 'B'),
    (3, 'C'),
    (5, 'G'),
    (2, 'E')
]

How can I output a DataFrame with only those particular cells highlighted?

I know I can pass the position of a single cell to .style.applymap() to highlight it as such:

# Style the DF
(
    df
    .style
    .applymap(lambda x: "background-color: yellow", subset=cells_to_highlight[0])
)

enter image description here

But how can I pass multiple cells to this method?

mpjan
  • 1,790
  • 5
  • 18
  • 21
  • 2
    Does [Highlighting multiple cells in different colors with Pandas](https://stackoverflow.com/questions/41555678/highlighting-multiple-cells-in-different-colors-with-pandas) nswer your question? – wwii Dec 20 '21 at 21:46
  • Yes it does! Thanks! – mpjan Dec 21 '21 at 10:19

0 Answers0