【问题标题】:Compare row value with column name and highlight the intersecting cell in Pandas将行值与列名进行比较并突出显示 Pandas 中的相交单元格
【发布时间】:2020-06-11 12:07:16
【问题描述】:

我有一个这样的数据集,我想根据一行中的 date1 和 date2 值以及相应的列名突出显示相交的单元格 input dataset

sample output

【问题讨论】:

  • 这是 Excel 电子表格吗?
  • 不,它是 Pandas 数据框
  • Pandas 没有任何内置功能可以像这样突出显示输出中的单元格。正确的方法取决于您用来显示数据的程序。
  • @shadowtalker - 我们可以不使用条件格式吗?基本上,编写一个循环来识别相交的单元格,然后应用条件格式。我在 Jupyter Nb 中运行,需要以这种方式可视化
  • This 风格指南可能会有所帮助。

标签: python pandas matrix conditional-formatting


【解决方案1】:

我的评论实际上是不正确的。 Pandas 确实为 Jupyter 笔记本中的输出提供条件格式/条件样式。文档在这里:https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

实际上有几种方法可以实现这一点,但最简单的方法是检查每个单元格的值并设置其样式。

data = pd.DataFrame({
    'x': [1, 2, 3, 4],
    'y': [19, 10, 11, 12]
})

def yellow_highlight_max(series):
    is_max_val = series == series.max()
    return is_max_val.map({True: 'background-color: yellow', False: ''}).tolist()

data.style.apply(yellow_highlight_max)

您应该仔细查阅文档以了解您想要的用途。

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多