突出显示熊猫df中列的最大值

pesc9

我用以下代码突出显示了df的黄色最大值:

def highlight_max(s):
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

pivot_p.style.apply(highlight_max)

但是现在我要强调每列的5个最大值。我已经尝试了以下代码,但无法正常工作:

def highlight_large(s):
    is_large = s == s.nlargest(5)
    return ['background-color: yellow' if v else '' for v in is_large]

pivot_p.style.apply(highlight_large)

错误:

ValueError: ('Can only compare identically-labeled Series objects', 'occurred at index %_0')
亚历山大·B

你可以试试:

def highlight_max(s):
    is_large = s.nlargest(5).values
    return ['background-color: yellow' if v in is_large else '' for v in s]

完整示例:

# Import modules
import pandas as pd
import numpy as np

# Create example dataframe
pivot_p = pd.DataFrame({"a": np.random.randint(0,15,20),
                  "b": np.random.random(20)})

def highlight_max(s):
    # Get 5 largest values of the column
    is_large = s.nlargest(5).values
    # Apply style is the current value is among the 5 biggest values
    return ['background-color: yellow' if v in is_large else '' for v in s]

pivot_p.style.apply(highlight_max)

输出:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Pandas-在列中突出显示最大值

如何突出 df.plot.barh 中的最大值?

Excel:突出显示在相邻列中多次出现的多个值的最大值

列中的熊猫最大值减去

在熊猫列中查找最大值

如何比较两个不同的列并突出显示每行中的最大值?

VBA:突出显示各列中每一行的最大值(已解决)

熊猫在一列中找到最大值,然后从另一行中显示

如何在 Tableau 表仪表板列中突出显示最大值和最低成本值

用熊猫中的最大值填充列值

如何在多层groupby中划分熊猫中两个单独的df列的最大值?

获取熊猫中每列的最大值数

Pandas DF - 按 A 列中的值过滤 DF,B 列中的最大值(分组依据?)

将熊猫df中的所有值乘以组内的最大值

熊猫在行中的最大值,并用值和列名返回df

突出显示最小值/最大值表jquery

熊猫中最大值的列标签

画一个圆以突出显示R中曲线的最大值

在海上热图的一栏中突出显示最大值

突出显示在特定单元格中具有最大值的表行

在Excel中的行/列中仅突出显示所有最小值/最大值的第一项

如何用最大值突出显示单元格

熊猫-df.max(),多个最大值

熊猫:将列中的所有值替换为列中的最大值

熊猫创建显示行的绝对最大值的列,但保留负数

如何将多列中的最大值返回到pandas df中的新列

VBA Excel,如何提取特定列中的最大值并在特定工作表中显示最大值

几个df中的一列(第2列)的最大值

根据 groupby 场景熊猫中的最大值计数替换列值