我想根据在另一个等长列表中找到的值将不同的背景颜色应用于 DataFrame 上的列。我的数据(这是一个玩具示例)具有以下结构:
Username Password Indications New_name Mr/Mrs Balance
Carlos xxx LoreIpsum Corona Mrs 100
Daniel yyy LoreIpsum Corona Mrs 200
Guille zzz LoreIpsum Corona Mrs 300
我正在开发一个测试自动化框架。在某些时候,我需要从网站读取值(余额列)并将其与我从 excel 中读取的值进行比较。在我这样做之后,我将 True 或 False 附加到列表中。因此,如果前两个读取值等于我的电子表格上的数据,但第三个是错误的,我的列表将如下所示:
In: Print(checkList)
Out: [True, True, False]
我找到了如何通过以下命令将样式应用于一行:
df.style.applymap(lambda x: 'background-color: red' if Condition else 'background-color: green', subset=['Balance'])
我的问题是我不知道如何迭代行以及带有布尔值的列表,在上面的代码行中,对所有行都应用了相同的条件。如有必要,我可以提供进一步的解释。