Python Pandas: Highlight matching text and row

You can format conditionally rows or cells containing this text, e.g. as per below example. I don’t think you will be able to highlight in red just the part of the text (well, unless you want to go into significant hacks of reparsing entire html, which I’m not even sure would be possible). Refer to the Styler documentation.

import pandas as pd

df1 = [ 'i like to shop at store a.' , 'he likes to shop at the store b.', 'she is happy to shop at store c.', 'we want to shop at the store d.']
df2 = [ 'store a', 'store b', 'store c', 'store d' ]
df3 = [ 'like to', 'likes to shop', 'at store' ]

myDataSet = list(zip(df1,df2))
df = pd.DataFrame(data = myDataSet, columns=['df1', 'df2'])

def in_statements(val):
    for statement in df3:
        if statement in val:
            color="yellow"
            break
        else:
            color="black"
    return 'background-color: %s' % color

df = df.style.applymap(in_statements)

df

Why deal with the styling fuzz anyways? 🙂 not better just to add an extra column which extracts the text which is interesting for you? (or blank if it’s not there)

Edit:
Per request, a way to achieve the objective without styling limitations, by adding an extra column:

def check(df):
    df["Statements"] = ", ".join(
        [x for x in df3 if x in df["df1"].to_string()])
    return df

df = df.groupby("df1").apply(lambda dfx: check(dfx))
df

Leave a Comment