Let's say I have a pandas DataFrame and I use style.apply
to manipulate the styles:
# sample code by jezrael from https://stackoverflow.com/questions/41654949/pandas-style-function-to-highlight-specific-columns
import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
# print(data)
def highlight_cols(x):
# copy df to new - original data are not changed
df = x.copy()
# select all values to default value - red color
df.loc[:,:] = 'background-color: red'
# overwrite values grey color
df[['B','C']] = 'background-color: grey'
# return color df
return df
data = data.style.apply(highlight_cols, axis=None)
This however produces a class in each of the <td>
tag and then dynamically adds styling to each.
I want to add class red
for the first case, and grey
for the second, in terms of HTML (<td class="red">content</td>
) for further processing in my own .css file. Is it possible directly with pandas? If not, are there any libraries that will let me manipulate the HTML contents of the pandas DataFrame?
Thanks in advance for any help.