Python Pandas - Highlighting maximum value in column

后端 未结 3 2101
栀梦
栀梦 2021-01-04 22:01

I have a dataframe produced by this code:

hmdf = pd.DataFrame(hm01)
new_hm02 = hmdf[[\'FinancialYear\',\'Month\']]
new_hm01 = hmdf[[\'FinancialYear\',\'Month         


        
相关标签:
3条回答
  • 2021-01-04 22:35

    Variation highlighting max value column-wise (axis=1) using two colors. One color highlights duplicate max values. The other color highlights only the last column containing the max value.

    def highlight_last_max(data, colormax='antiquewhite', colormaxlast='lightgreen'):
        colormax_attr = f'background-color: {colormax}'
        colormaxlast_attr = f'background-color: {colormaxlast}'
        max_value = data.max()
        is_max = [colormax_attr if v == max_value else '' for v in data]
        is_max[len(data) - list(reversed(data)).index(max_value) -  1] = colormaxlast_attr
        return is_max
    
    df.style.apply(highlight_last_max,axis=1)
    
    0 讨论(0)
  • 2021-01-04 22:38

    If you are using Python 3 this should easily do the trick

    dfPercent.style.highlight_max(color = 'yellow', axis = 0)
    
    0 讨论(0)
  • 2021-01-04 22:51

    There is problem you need convert values to floats for correct max, because get max value of strings - 9 is more as 1:

    def highlight_max(data, color='yellow'):
        '''
        highlight the maximum in a Series or DataFrame
        '''
        attr = 'background-color: {}'.format(color)
        #remove % and cast to float
        data = data.replace('%','', regex=True).astype(float)
        if data.ndim == 1:  # Series from .apply(axis=0) or axis=1
            is_max = data == data.max()
            return [attr if v else '' for v in is_max]
        else:  # from .apply(axis=None)
            is_max = data == data.max().max()
            return pd.DataFrame(np.where(is_max, attr, ''),
                                index=data.index, columns=data.columns)
    

    Sample:

    dfPercent = pd.DataFrame({'2014/2015':['10.3%','9.7%','9.2%'],
                       '2015/2016':['4.8%','100.8%','9.7%']})
    print (dfPercent)
      2014/2015 2015/2016
    0     10.3%      4.8%
    1      9.7%    100.8%
    2      9.2%      9.7%
    

    Command:

    dfPercent.style.apply(highlight_max)
    

    0 讨论(0)
提交回复
热议问题