I have found below code snippet in the forum. It is creating a dataframe with a colormap that is centered around zero. This is what I am looking for as well, however, the first three columns of my dataframe are text/string columns. So when I run below code it returns ‘<=’ not supported between instances of ‘str’ and ‘float’.
Is there a way to apply the code below to a subset of a dataframe only? And if so, can you help me on the exact syntax?
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import colors
np.random.seed(24)
df = pd.DataFrame()
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4)*10, columns=list('ABCD'))],
axis=1)
df.iloc[0, 2] = 0.0
cm = sns.diverging_palette(5, 250, as_cmap=True)
divnorm = colors.TwoSlopeNorm(vmin=df.values.min(), vcenter=0, vmax=df.values.max())
df.style.background_gradient(cmap=cm, axis=None, vmin=0, vmax=1, gmap=df.apply(divnorm))