python - Python pandas - 根据2列的值对行进行样式化/突出显示
问题描述
如果两列中的任何一列(row2/row3 或两者)中有空数据,我试图突出显示该行,以便我可以更容易地看到它。
例如:
col1 col2 col3
a 23 16
b 12
c 12
d
返回结果应该是以黄色突出显示的 b、c、d 行。知道如何实现这一目标吗?谢谢!
解决方案
使用自定义函数:
def highlight(x):
c1 = 'background-color: yellow'
# condition
m = x.isna().any(axis=1)
#if need specify columns for check
m = x[['col2','col3']].isna().any(axis=1)
#print (m)
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
return df1.mask(m, c1)
df.style.apply(highlight, axis=None)
推荐阅读
- python - 如何向客户端提交 map_blocks 操作
- javascript - 如何连接不定数量的Javascript对象数组
- database - 在 Hibernate 4 中具有 3 个实体的 SQLite 查询(嵌套连接?)
- symfony - 学说映射导入视图
- angular - 如何提高渲染性能?
- python - 如何在 FLASK 中将 url 作为 REST GET 参数传递
- azure - 哪种 azure 服务计划最适合托管高流量的 SPA
- sql - 尝试使用 sqlalchemy 将 sql 表迁移到新格式。几行之后,它在一个给出错误“ORA-00911:无效字符”时失败
- jenkins - Jenkins 项目无法连接到存储库
- r - 将2个向量组合成R中的矩阵时如何保留/分配行名和列名