'How to highlight character of cell using pandas dataframe?

I know that it is possible highlight dataframe cell, but I want highlight only one character from one column or cell.

By example, from column a show second letter in other color (e.g. red, black,...)

a b c 0 demo 2 3 1 demo 5 6 2 demo 8 9

How can I achieved it?



Solution 1:[1]

You can change the string content to HTML first and display it:

import pandas as pd
from IPython.display import HTML
df = pd.DataFrame({'a': ['demo'] * 3, 'b': [2, 5, 8], 'c': [1,2,9]})
df['a'] = df['a'].str[:1] + '<span style="color: #ff0000">' + df['a'].str[1] + '</span>' + df['a'].str[2:]
HTML(df.to_html(escape=False,))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Z Li