Python Pandas-在列中突出显示最大值 [英] Python Pandas - Highlighting maximum value in column
本文介绍了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)
然后此代码:dfPercent.style.apply(highlight_max)
生成此代码:
And then this code: dfPercent.style.apply(highlight_max)
produces this:
如您所见,只有第一列和最后一列突出显示了正确的最大值.
As you can see, only the first and last column have the correct max value highlighted.
任何人都知道出了什么问题吗?
Anyone know what is going wrong?
谢谢
推荐答案
存在一个问题,您需要将值转换为浮点数以获取正确的max
,因为获取字符串的最大值-9
更多地是1
:
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)
示例:
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%
这篇关于Python Pandas-在列中突出显示最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文