1

I'm trying to colour columns "Col2", "Col3", "Col4", "Col5", "Col6" based on Col1 and return other columns without any colour.

Sample Data

from random import randint

x = [randint(0, 1) for p in range(0, 10)]

sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
               "Col2": [randint(0, 1) for p in range(0, 10)],
               "Col3": [randint(0, 1) for p in range(0, 10)],
               "Col4": [randint(0, 1) for p in range(0, 10)],
               "Col5": [randint(0, 1) for p in range(0, 10)],
               "Col6": [randint(0, 1) for p in range(0, 10)]}
abcd = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
sample = pd.DataFrame(sample_dict)
data = pd.concat([sample, abcd], axis=1)

Col1    Col2    Col3    Col4    Col5    Col6    A   B   C   D
0   0   1   1   1   0   1   -1.358769   -0.310314   -1.056405   -0.567577
1   0   1   0   1   1   0   -0.539893   -0.139629   0.270759    -0.430564
2   1   1   1   0   1   0   -0.009886   0.023482    0.966884    0.612875
3   0   1   1   1   0   1   0.962987    0.191210    -0.228937   -0.338486
4   1   1   1   0   0   1   -0.867326   -0.461046   0.832390    0.956392
5   0   0   0   0   1   1   0.348276    0.711152    -1.016820   0.394526
6   1   1   1   1   0   0   0.622317    0.519261    -2.022494   -1.170836
7   0   1   0   0   1   0   0.033249    0.491181    -0.065532   0.936868
8   1   0   0   1   1   1   1.064310    -0.257726   -0.197229   0.348314
9   0   1   0   1   1   0   0.017713    -0.624656   -0.341611   -1.433317

So far, I'm able to colour it by

data["Col1", "Col2", "Col3", "Col4", "Col5", "Col6"].style.apply(lambda x: ["background-color: orange" if v != x.iloc[0] else "background_color: none" for v in x], axis=1)

enter image description here

but unable to return the other columns.

How to add colour to columns "Col2", "Col3", "Col4", "Col5", "Col6" based on Col1? Iss it possible to apply different color for each column in "Col2", "Col3", "Col4", "Col5", "Col6"?

1 Answer 1

2

You could iterate per column and use a dictionary of colors:

colors = {'Col2': 'orange',
          'Col3': 'lightblue',
          'Col4': 'lightgreen',
          'Col5': 'lightpink',
          'Col6': 'yellow'
         }

import numpy as np
(data
 .style.apply(lambda c: np.where(sample['Col1'].eq(c),
                                 'background-color: none',
                                f'background-color: {colors.get(c.name, "none")}')
             )
)

output:

table

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

5 Comments

@Pluviophile I already updated the answer ;) (but this was already demonstrating the effect with Col1)
Yes it maps the colors from the dictionary for the defined columns and uses no color instead
I have large data, Just wanted to check the code only looks for keys/columns that are defined in the dictionary right?
No, it will currently evaluate all columns and set None, but if you want open a new question to not pollute this one (I thought the main topic was the multiple colors) and I can provide an alternative
Could you please provide an alternate here?

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.