Skip to content

Instantly share code, notes, and snippets.

@sauravmishra1710
Last active March 17, 2020 06:37
Show Gist options
  • Save sauravmishra1710/980b9d90f8616902ee3ecf1e6550dd4b to your computer and use it in GitHub Desktop.
Save sauravmishra1710/980b9d90f8616902ee3ecf1e6550dd4b to your computer and use it in GitHub Desktop.
highlight the maximum in a Series or DataFrame
************************************************************************************************************
'''highlight the maximum in a Series or DataFrame'''
# For more on dataframe styling refer - https://www.kaggle.com/nxrprime/styling-data-frames-covid-19-vs-conferences
************************************************************************************************************
def highlight_max(data, color='red'):
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)
************************************************************************************************************
#df.style.apply(highlight_max,subset=['col1', 'col2'])
#df.style.apply(highlight_max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment