如何根据不同数据帧中的列的值突出显示一个数据帧中的行或字符串?

问题描述 投票:0回答:1

我希望能够从数据帧的一列中获取值,并在另一个数据帧中找到完全相同的值,以突出显示整行或仅突出显示字符串(无论哪种更容易/可能)。

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)

如果可能的话,我想做一些看起来像底部示例中的任何一个的东西。

我已经尝试了很多不同的东西,但我仍在学习Python,所以不知道如何做到这一点。预先感谢您!

python pandas highlight conditional-formatting styling
1个回答
0
投票

您可以检查

df1
isin
的基因是否是
df2
的基因,然后
apply
你的 CSS 样式 :

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

两者预览:

© www.soinside.com 2019 - 2024. All rights reserved.