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
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']).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With