'Color Specific columns based on condition of another column

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"?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source