1

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.

brungesh
  • 11
  • 2
  • 2
    Does this answer your question? [How to define color of specific cell in pandas dataframe based on integer position (e.g., df.iloc\[1,1\]) with df.style?](https://stackoverflow.com/questions/52887800/how-to-define-color-of-specific-cell-in-pandas-dataframe-based-on-integer-positi) – Rabinzel Jun 16 '22 at 21:27
  • This solution is realated to coloring the dataframe cells using 'background-color'. In my case I need to partially color the text inside the dataframe cell. – brungesh Jun 18 '22 at 00:03

0 Answers0