2017-08-10 45 views
1

我由此代碼生成一個數據幀: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)產生這樣的:

enter image description here

正如您所看到的,只有第一列和最後一列纔會突出顯示正確的最大值。

任何人都知道發生了什麼問題?

謝謝

+0

嘗試'dfPercent.style.applymap(highlight_max)' –

+0

嘿,得到一個錯誤:AttributeError:(「'str'對象沒有屬性'ndim'」,u'ccurred在index 2014/2015')? – ScoutEU

+1

它似乎也應用在列名稱上的功能... –

回答

4

有問題,你需要將值轉換爲浮動正確max,因爲得到的字符串的最大值 - 9更是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% 

jupyter

+0

嘿jezrael。謝謝。我得到一個'ValueError:'('無效的文字爲float():9.2%',u'ccurred在索引2014/2015')'錯誤? – ScoutEU

+1

給我一些時間,我必須測試它。 – jezrael

+0

謝謝!我真的很感激它:) – ScoutEU