Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colouring one column of pandas dataframe

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

like image 806
Penny Pang Avatar asked Jun 06 '17 10:06

Penny Pang


People also ask

How do you color a column in pandas?

With your one line of code, can you apply to several columns with different colors for each column? @sqllearner you can apply the same color to several columns just by adding them to the subset, like df. style. set_properties(**{'background-color': 'red'}, subset=['A', 'C']).

How do I highlight a column in pandas?

You access the DataFrame's style property and to every column specified to highlight, you set “background-color: lightgreen” to their style configuration. This function returns a Styler object and not a DataFrame. For this and other styling options take a look at pandas style module.

How do I match column values in pandas?

By using the Where() method in NumPy, we are given the condition to compare the columns. If 'column1' is lesser than 'column2' and 'column1' is lesser than the 'column3', We print the values of 'column1'. If the condition fails, we give the value as 'NaN'. These results are stored in the new column in the dataframe.


1 Answers

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

like image 147
lrvlr Avatar answered Oct 04 '22 03:10

lrvlr