我想根据另一个等长列表中的值,对数据帧上的列应用不同的背景色。我的数据(这是一个玩具示例)具有以下结构:
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'])
我的问题是,我不知道如何迭代行以及布尔值列表,在上面的代码行中,对所有行应用相同的条件。如有必要,我可以提供进一步的解释
如果@jezrael solution返回:(TypeError:_translate()缺少2个必需的位置参数:“稀疏索引”和“稀疏列”)
将熊猫降级到1.2.4版可能是一个临时解决方案
然后,您可以尝试创建由} 中的列表中创建@jezrael solution
background-color
按条件填充的DataFrame
,例如从^{使用
df.loc
代替np.where
的替代解决方案:您可以根据条件创建由} 中的列表:
background-color
填充的DataFrame
,例如从^{相关问题 更多 >
编程相关推荐