How to color partial string in a dataframe cell

339 views Asked by At

I am trying to color a string partially in a dataframe cell in python. Here is a similar sample code of the scenario,

def style_red(x):
    if not x.isdigit():
        return 'color:red;'

# creating a DataFrame
dict = {'Name' : ['Martha BR', 'Bran dffd', 'Rob eqr', 'Georgia fdaf'],
        'Maths' : [87, 91, 97, 95],
        'Science' : [83, 99, 84, 76]}
df1 = pd.DataFrame(dict)
df1.style.applymap(lambda x: style_red(str(x)))

The above code will color the entire text in the 'Name' column. Here I want to color only the last names and not the entire names. I want to partially color the text in the dataframe cell.

0

There are 0 answers