3

Pandas style keyword only works on rows or columns. Is it possible to split it according to sublevels.

For example

np.random.seed(24)
df = pd.DataFrame({'Types': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=['Names','V1','V2','V3'])],
               axis=1)

df['Types'][0:7] ="Dang"

df['Types'][7:] ="Fang"

df['Names'][0:3] ="Andy"

df['Names'][3:8] ="Flower"

df['Names'][8:] ="Avril"

df2 = pd.groupby(df,['Types','Names']).mean()
df2

Now I want to highlight the max value according to sublevel

def highlight_max(x):
     return ['background-color: yellow' if v == x.max() else ''
                for v in x]

df2.style.apply(highlight_max,axis=0,subset=['V1'])

It will highlight in this case max value in column 'V1'. I want basically two max values as per the group levels. So I want to highlighted values. Is there any simple way to do that? None of the intro material covers this basic feature.

1 Answer 1

4

You need return DataFrame of colors for set styles. So need create new df with same index and columns with default values - here empty string and then change values by condition created by GroupBy.transform and compare by eq (==):

def highlight_max(x): 
   c1 = 'background-color: yellow'
   c2 = '' 
   m = x.groupby(level=0)['V1'].transform('max').eq(x['V1'])

   df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
   df1.loc[m, 'V1'] = c1
   return df1

df2.style.apply(highlight_max,axis=None)

pic

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.