Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas - Highlighting maximum value in column

Tags:

python

pandas

I have a dataframe produced by this code:

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

hm05 = new_hm01.pivot_table(index=['FinancialYear','Month'], aggfunc='count')
vals1 = ['April    ', 'May      ', 'June     ', 'July     ', 'August   ', 'September', 'October  ', 'November ', 'December ', 'January  ', 'February ', 'March    ']

df_hm = new_hm01.groupby(['Month', 'FinancialYear']).size().unstack(fill_value=0).rename(columns=lambda x: '{}'.format(x))
df_hml = df_hm.reindex(vals1)

And then I have a function to highlight the maximum value in each column:

def highlight_max(data, color='yellow'):
    '''
    highlight the maximum in a Series or DataFrame
    '''
    attr = 'background-color: {}'.format(color)
    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)

And then this code: dfPercent.style.apply(highlight_max) produces this:

enter image description here

As you can see, only the first and last column have the correct max value highlighted.

Anyone know what is going wrong?

Thank you

like image 640
ScoutEU Avatar asked Aug 10 '17 06:08

ScoutEU


People also ask

How do you find the maximum value of a DataFrame column in Python?

Pandas DataFrame max() Method The max() method returns a Series with the maximum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the maximum value for each row.

How do you find the maximum value of a specific column?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

Where is Max value in Panda series?

In the pandas series constructor, there is a method called argmax() which is used to get the position of maximum value over the series data. The pandas series is a single-dimensional data structure object with row index values. By using row index values we can access the data.

How do you color a column in pandas?

@sqllearner you can apply the same color to several columns just by adding them to the subset, like df. style. set_properties(**{'background-color': 'red'}, subset=['A', 'C']).


2 Answers

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

dfPercent.style.highlight_max(color = 'yellow', axis = 0)
like image 115
Caio Franco Avatar answered Sep 28 '22 07:09

Caio Franco


Easy way to color max, min, or null values in pandas.DataFrame is to uses style of pandas.DataFrame.style, which Contains methods for building a styled HTML representation of the DataFrame. Here is an example:

  • Color Max Values: your_df.style.highlight_max(color = 'green')
  • Color Min Values: your_df.style.highlight_min(color = 'red')
  • Color Null values: your_df.highlight_null(color = 'yellow')
  • If you want to apply all in the same output:
    your_df.style.highlight_max(color='green').highlight_min(color='red').highlight_null(null_color='yellow')
like image 44
Ilyas Avatar answered Sep 28 '22 06:09

Ilyas