Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting multiple cells in different colors with Pandas

Imagine we have a dataframe and I want to color different cells:

  • Cells ['Arizona','company'](1st), ['Texas','size'](1099) as green.
  • Cells ['Florida','veterans'](26), ['Maine','armored'](0) as red.

What's a good way to do it?

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
            'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
            'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],
            'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],
            'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],
            'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],
            'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],
            'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],
            'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
            'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']}

df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters', 'origin'])

df = df.set_index('origin')

df.head()

(http://chrisalbon.com/python/pandas_indexing_selecting.html)

like image 527
thatMeow Avatar asked Jan 09 '17 19:01

thatMeow


1 Answers

You can use slicing in Style with parameter subset and function Styler.applymap for elementwise styles, run code in jupyter notebook:

import pandas as pd
import numpy as np

def red(val):
    color = 'red'
    return 'background-color: %s' % color

def green(val):
    color = 'green'
    return 'background-color: %s' % color

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
            'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
            'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],
            'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],
            'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],
            'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],
            'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],
            'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],
            'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
            'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']}

df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters', 'origin'])

df = df.set_index('origin')
print (df)

df.style.applymap(green, subset=pd.IndexSlice['Arizona':'Texas', 'company': 'size'])
        .applymap(red, subset=pd.IndexSlice['Florida':'Maine', 'veterans': 'armored'])

If need change only some values in DataFrame, you can use Styler.apply with axis=None for tablewise styles, also the function must return a DataFrame with the same index and column labels:

def create_colors(x):
    #copy df to new - original data are not changed
    df1 = x.copy()
    #select all values to default value - no color
    df1.loc[:,:] = 'background-color: '
    #overwrite values with green and red color
    df1.loc['Arizona', 'company'] = 'background-color: green'
    df1.loc['Texas', 'size'] = 'background-color: green'
    df1.loc['Florida', 'veterans'] = 'background-color: red'
    df1.loc['Maine', 'armored'] = 'background-color: red'
    #return color df
    return df1      

df.style.apply(create_colors, axis=None)

table1

like image 143
jezrael Avatar answered Sep 19 '22 12:09

jezrael