Reputation: 545
I have a dataframe and would like to use the .style
to highlight the first column.
I wasn't sure if there is a loop I have to use or a function
Upvotes: 12
Views: 29161
Reputation: 181
You can solve it in one line like this:
df.style.set_properties(**{'background-color': 'red'}, subset=['A'])
where subset is the list of column names on which you want to apply the desired properties.
The result is the same as shown by @jezrael You can check other properties and possibilities for styling in pandas' website
Upvotes: 18
Reputation: 862406
I think you need custom function which return DataFrame
with color for first column:
np.random.seed(100)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
def highlight_col(x):
r = 'background-color: red'
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
df1.iloc[:, 0] = r
return df1
df.style.apply(highlight_col, axis=None)
Upvotes: 6