<jupyter_start><jupyter_text>Styling* [Reference: Style](https://pandas.pydata.org/docs/reference/style.html)* [User Guide: Styling](https://pandas.pydata.org/docs/user_guide/style.html)  Data<jupyter_code>import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan
df<jupyter_output><empty_output><jupyter_text>B: Beginner  B1: Can you highlight negative values in red? (as shown below)![](../../../images/Pandas/style_b1_highlight_negative.png)<jupyter_code>def color_negative_red(val):
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color

df.style.applymap(color_negative_red)<jupyter_output><empty_output><jupyter_text>B2: Can you highlight maxiumum value in the whole DataFrame?  B3: Can you highlight maxiumum value per column? row?  A: Advanced  A1: Can you highlight columns based on the column name?Sample color map:`{'A':'cyan', 'B':'lightblue', 'C':'lightyellow', 'D':'salmon', 'E':'lightgreen'}`![](../../../images/Pandas/style_a1_color_columns.png)<jupyter_code>coldict = {'A':'cyan', 'B':'lightblue', 'C':'lightyellow', 'D':'salmon', 'E':'lightgreen'}

def highlight_cols(s, coldict):
    if s.name in coldict.keys():
        return ['background-color: {}'.format(coldict[s.name])] * len(s)
    return [''] * len(s)

df.style.apply(highlight_cols, coldict=coldict)<jupyter_output><empty_output><jupyter_text>A2: Can you color rows which index is divisible to 3 and 5?  M: Master<jupyter_code>df_m = pd.DataFrame({'A': [12,6,8, 15], 'B': [3,4,1, 7]})<jupyter_output><empty_output><jupyter_text>M1Can you add new column B to A % and style it as follows(bar dependent on value lenght):Can you change the scale to 100%?![](../../../images/Pandas/style_m1_styling_bar.png)<jupyter_code>df_m['B/A %'] = df_m['B'] / df_m['A'] * 100
df_m.style.bar(subset=["B/A %"], color='#FFA07A')<jupyter_output><empty_output><jupyter_text>M2Can you add a new column which is sum of both A and B?Then style it by showing split based on the percent of which value from column A or B as shown below:![](../../../images/Pandas/style_m1_styling_split_color.png)<jupyter_code>def highlight_cols(x):
    df = x.copy()
    
    for i in df.iterrows():
        b = i[1].get('B')
        a = i[1].get('A')
        
        if a != 0:
            grad = b / a * 100
        else:
            grad = 0
        
        df.loc[i[0],['total']] = f'width:  10em; height:  80%; background: linear-gradient(to right, tomato {grad}%, lightblue {grad}%);'
        
    df.iloc[:,0:3] = ''
#     display(df)
    return df    

df_m['total'] = df_m['B'] + df_m['A']
df_m.style.apply(highlight_cols, axis=None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1773851590106007, 'fraction_numerical': 0.0215547703180212, 'mean_word_length': 4.232902033271719, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '601189', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1056, 'n_words': 236, 'path': 'Pandas Textbook/Styling/Styling.ipynb', 'repo_name': 'softhints/Data-Science-Textbooks', 'size': '2830'}