1

I want to be able take vales from one column of dataframe and find the exact same values in another dataframe to either highlight the whole row or just the string (whatever is easier/possible).

dataset1 = {'Gene Name':['ampC','fliC','ompC','fruB','yobH','gltP','ruvA','yacC','folD'],
       'FoldChange':['4.54','5.65','7.89','6.45','10.67','4.63','2.65','9.45','5.79'],
        'pvalue':['0','0','0','0','0','0','0','0','0']}

df1= pd.DataFrame(dataset1)

dataset2 = {'Gene Name':['gltP','ruvA','yacC','folD']}

df2 = pd.DataFrame(dataset2)

If it is possible I would like to make something that would look like either of the bottom examples.

What I would like my dataset to look like

I've tried a bunch of different stuff but I'm still learning python so not sure how to do this.

1 Answer 1

2

You can check if the genes of df1 isin those of df2, then apply your CSS style :

m = df1["Gene Name"].isin(df2["Gene Name"])

def fn(ser, how):
    import numpy as np
    return np.where(m, f"{how}: red", "")
    
# choose one of the two
left = df1.style.apply(fn, how="color", subset=["Gene Name"])
right = df1.style.apply(fn, how="background-color")

# .to_excel("output.xlsx", index=False) # to save an Excel

Preview of both :

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Thank you so much, they way you wrote the solution was very clear and easy for me to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.