在MultiIndex DataFrame-Pandas上突出显示最大/最小

时间:2019-04-15 11:27:56

标签: python pandas dataframe multi-index

假定具有2层MultiIndex数据框:

df = pd.DataFrame([['one', 'A', 100,3], ['two', 'A', 101, 4], 
                   ['three', 'A', 102, 6], ['one', 'B', 103, 6], 
                   ['two', 'B', 104, 0], ['three', 'B', 105, 3]],
   columns=['c1', 'c2', 'c3', 'c4']).set_index(['c1', 'c2']).sort_index()
print(df)

看起来像这样

           c3  c4
c1    c2         
one   A   100   3
      B   103   6
three A   102   6
      B   105   3
two   A   101   4
      B   104   0

我的目标是针对'c2''c3'中所有列的'c4'元素之间的最小值(或等效最大值)(以Pandas的样式突出显示) {1}}

'c1'

您有什么建议吗?

我已经尝试过了,但是它是按列工作的,而不是基于索引的。

             c3      c4
c1    c2         
one   A   **100**   **3**
      B     103       6
three A   **102**     6
      B     105     **3**
two   A   **101**     4
      B     104     **0**

结果如下

def highlight_min(data):

    attr = 'background-color: {}'.format(color)

    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1
        is_max = data == data.min()
        return [attr if v else '' for v in is_max]
    else:  # from .apply(axis=None)
        is_max = data == data.min().min()
        return pd.DataFrame(np.where(is_max, attr, ''),
                            index=data.index, columns=data.columns)

df = df.style.apply(highlight_min, axis=0)

1 个答案:

答案 0 :(得分:2)

GroupBy.transformmin一起使用,并按所有值进行比较:

def highlight_min(data):
    color= 'red'
    attr = 'background-color: {}'.format(color)

    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1
        is_min = data == data.min()
        return [attr if v else '' for v in is_min]
    else: 
        is_min = data.groupby(level=0).transform('min') == data
        return pd.DataFrame(np.where(is_min, attr, ''),
                            index=data.index, columns=data.columns)
相关问题