Home > Blockchain >  How to highlight character of cell using pandas dataframe?
How to highlight character of cell using pandas dataframe?

Time:03-18

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?

CodePudding user response:

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

  • Related