在 Pandas DataFrame 中突出显示列中的前 n 个最大值

3
最初的回答:

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

我用以下代码将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')
1个回答

3

您可以尝试:

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)

输出:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接