如何使用 pandas 应用具有条件的不同样式
How to apply different styles with a condition using pandas
我想根据在另一个等长列表中找到的值,将不同的背景颜色应用于 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
我正在开发一个测试自动化框架。在某些时候,我需要从网站读取值(balance 列)并将其与我从 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'])
我的问题是我不知道如何使用布尔值遍历行和列表,上面的代码行对所有行应用相同的条件。如有必要,我可以提供进一步的解释。
您可以根据条件创建由 background-color
填充的 DataFrame
,例如来自 Styler.apply
中的列表:
checkList = [True, True, False]
def highlight(x):
c1 = 'background-color: red'
c2 = 'background-color: green'
#if necessary pass condition
#checkList = x['Balance'] < 300
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set Balance column by condition in list (necessary same length like df)
df1['Balance'] = np.where(checkList, c1, c2)
return df1
df.style.apply(highlight, axis=None)
如果@jezrael 解决方案 returns 出局:(TypeError:_translate() 缺少 2 个必需的位置参数:'sparse_index' 和 'sparse_cols')
将 pandas 降级到 1.2.4 版本可能是一个临时解决方案...
# Uninstall any pandas library installed:
pip uninstall pandas
# After uninstalling pandas, install pandas==1.2.4
pip install pandas==1.2.4
然后你可以尝试创建 DataFrame
由 background-color
填充的条件,例如来自你在 Styler.apply
中的列表,作为 @jezrael 解决方案。
使用 df.loc
而不是 np.where
的替代解决方案:
checkList = [True, True, False]
def highlight(x):
c1 = 'background-color: red'
c2 = 'background-color: green'
# If necessary pass condition
checkList = x['Balance'] <= 300
checkList2 = x['Balance'] > 300
# Empty DataFrame of styles
df1 = pd.DataFrame(x, index=x.index, columns=x.columns)
#set Balance column by condition in checkList (using df1.loc instead of np.where)
df1.loc[checkList, 'Balance'] = c1
df1.loc[chekcList2, 'Balance'] = c2
# Return styled dataset
return df1
# To apply highlight styler:
df.style.apply(highlight, axis=None)
我想根据在另一个等长列表中找到的值,将不同的背景颜色应用于 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
我正在开发一个测试自动化框架。在某些时候,我需要从网站读取值(balance 列)并将其与我从 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'])
我的问题是我不知道如何使用布尔值遍历行和列表,上面的代码行对所有行应用相同的条件。如有必要,我可以提供进一步的解释。
您可以根据条件创建由 background-color
填充的 DataFrame
,例如来自 Styler.apply
中的列表:
checkList = [True, True, False]
def highlight(x):
c1 = 'background-color: red'
c2 = 'background-color: green'
#if necessary pass condition
#checkList = x['Balance'] < 300
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set Balance column by condition in list (necessary same length like df)
df1['Balance'] = np.where(checkList, c1, c2)
return df1
df.style.apply(highlight, axis=None)
如果@jezrael 解决方案 returns 出局:(TypeError:_translate() 缺少 2 个必需的位置参数:'sparse_index' 和 'sparse_cols')
将 pandas 降级到 1.2.4 版本可能是一个临时解决方案...
# Uninstall any pandas library installed:
pip uninstall pandas
# After uninstalling pandas, install pandas==1.2.4
pip install pandas==1.2.4
然后你可以尝试创建 DataFrame
由 background-color
填充的条件,例如来自你在 Styler.apply
中的列表,作为 @jezrael 解决方案。
使用 df.loc
而不是 np.where
的替代解决方案:
checkList = [True, True, False]
def highlight(x):
c1 = 'background-color: red'
c2 = 'background-color: green'
# If necessary pass condition
checkList = x['Balance'] <= 300
checkList2 = x['Balance'] > 300
# Empty DataFrame of styles
df1 = pd.DataFrame(x, index=x.index, columns=x.columns)
#set Balance column by condition in checkList (using df1.loc instead of np.where)
df1.loc[checkList, 'Balance'] = c1
df1.loc[chekcList2, 'Balance'] = c2
# Return styled dataset
return df1
# To apply highlight styler:
df.style.apply(highlight, axis=None)