如何根据 Pandas 中的行值和列值突出显示单元格?
How can I highlight cells based on row and column values in Pandas?
我正在尝试突出显示我的行名称和行值。
如果 currentRatio 大于 1.4 高亮红色
如果 quickRatio 超过 32 则突出显示黄色
我在第一次测试 运行 时遇到了问题。
import pandas as pd
df1 = pd.DataFrame({'currentRatio' : [1.2, 1.5, 1.4, 2.0],
'quickRatio' : [30, 37, 30, 35], })
print(df1.T)
这个输出
0 1 2 3
currentRatio 1.2 1.5 1.4 2.0
quickRatio 30.0 37.0 30.0 35.0
def color_row(row):
pprint(row)
if row.name == 'currentRatio:
if row.value > 1.4:
return pd.Series('background-color: red', row.index)
display(df1.style.apply(color_row, axis=1))
pprint 行输出为
index currentRatio
0 1.2
1 1.5
2 1.14
3 1.2
4 1.352669417512594
感谢您抽出宝贵时间提供帮助
您可以使用适当的条件逻辑来理解列表
df1 = pd.DataFrame({'currentRatio' : [1.2, 1.5, 1.4, 2.0],
'quickRatio' : [30, 37, 30, 35], })
def color_row(row):
return ['background-color: red' if v>=1.4 and i=="currentRatio"
else 'background-color: yellow' if v>=32 and i=="quickRatio"
else ""
for i, v in row.items()]
df1.style.apply(color_row, axis=1)
我正在尝试突出显示我的行名称和行值。
如果 currentRatio 大于 1.4 高亮红色
如果 quickRatio 超过 32 则突出显示黄色
我在第一次测试 运行 时遇到了问题。
import pandas as pd
df1 = pd.DataFrame({'currentRatio' : [1.2, 1.5, 1.4, 2.0],
'quickRatio' : [30, 37, 30, 35], })
print(df1.T)
这个输出
0 1 2 3
currentRatio 1.2 1.5 1.4 2.0
quickRatio 30.0 37.0 30.0 35.0
def color_row(row):
pprint(row)
if row.name == 'currentRatio:
if row.value > 1.4:
return pd.Series('background-color: red', row.index)
display(df1.style.apply(color_row, axis=1))
pprint 行输出为
index currentRatio
0 1.2
1 1.5
2 1.14
3 1.2
4 1.352669417512594
感谢您抽出宝贵时间提供帮助
您可以使用适当的条件逻辑来理解列表
df1 = pd.DataFrame({'currentRatio' : [1.2, 1.5, 1.4, 2.0],
'quickRatio' : [30, 37, 30, 35], })
def color_row(row):
return ['background-color: red' if v>=1.4 and i=="currentRatio"
else 'background-color: yellow' if v>=32 and i=="quickRatio"
else ""
for i, v in row.items()]
df1.style.apply(color_row, axis=1)