How to color parts only of a Pandas dataframe columns?

420 Views Asked by At

I've a Pandas DataFrame with continuous sequence of ones and zeroes, as follows:

import numpy as np
import pandas as pd

m = np.array([[1, 1, 1, 1], [1, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 0]])
df = pd.DataFrame(m, columns=["C1", "C2", "C3", "C4"])
df.insert(0, "Sy", ["r1", "r2", "r3", "r4"])

Which gives me the following df:

   Sy  C1  C2  C3  C4
0  r1   1   1   1   1
1  r2   1   1   1   0
2  r3   1   0   1   0
3  r4   1   0   0   0

I'm trying to color only the series of ones in each column with different column specific colors. The series starts at row=0 and continues till the first zero appears. I took the help of this Stack Overflow post to color the columns.

However, this code colors the whole column and not just the cells containing consecutive sequence of 1's:

def f(dat, c="red"):
    return [f"background-color: {c}" for i in dat]


columns_with_color_dictionary = {
    "C1": "red",
    "C2": "blue",
    "C3": "orange",
    "C4": "yellow",
}

style = df.style
for column, color in columns_with_color_dictionary.items():
    style = style.apply(f, axis=0, subset=column, c=color)

with open("dd.html", "w") as fh:
    fh.write(style.render())

The Html output: enter image description here

Can anyone help me in this matter? Any alternative ideas are welcome too. The actual matrix is around 200X200 and I don't want color printing to console. Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

Here is one way to do it.

Replace:

style = df.style
for column, color in columns_with_color_dictionary.items():
    style = style.apply(f, axis=0, subset=column, c=color)

with:

style = df.style
for column, color in columns_with_color_dictionary.items():
    style = style.apply(f, axis=0, subset=column, c=color).applymap(
        lambda x: f"background-color: white",
        subset=(
            df[df[column] != 1].index,
            [column],
        ),
    )

And here is the Html output:

enter image description here