我由此代码生成一个数据帧:Python的熊猫 - 在柱突出最大值
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)
然后我有一个函数来突出各列中的最大值:
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)
产生这样的:
正如您所看到的,只有第一列和最后一列才会突出显示正确的最大值。
任何人都知道发生了什么问题?
谢谢
尝试'dfPercent.style.applymap(highlight_max)' –
嘿,得到一个错误:AttributeError:(“'str'对象没有属性'ndim'”,u'ccurred在index 2014/2015')? – ScoutEU
它似乎也应用在列名称上的功能... –