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())
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
Here is one way to do it.
Replace:
with:
And here is the Html output: