Carlos Hernandez Perez
Carlos Hernandez Perez

Reputation: 331

How to apply different styles with a condition using pandas

I want to apply different background colours to a column on a DataFrame according to the value found on another, equal length list. My data (this is a toy example) has the following structure:

Username    Password    Indications New_name    Mr/Mrs  Balance
Carlos       xxx         LoreIpsum  Corona      Mrs     100
Daniel       yyy         LoreIpsum  Corona      Mrs     200
Guille       zzz         LoreIpsum  Corona      Mrs     300

I am working on a test automation framework. At some point I need to read the values (the balance column) from a website and compare it to the values that I read from an excel. After I do so I append a True, or a False into a list. Thus if the first two read values are equal to the data on my spreadsheet but the third is wrong, my list would have this look:

In:  Print(checkList)
Out: [True, True, False]

I have found how to apply an style to a row via this command:

df.style.applymap(lambda x: 'background-color: red' if Condition else 'background-color: green', subset=['Balance'])

My problem is that I do not know how to iterate over the rows as well as the list with the booleans, on the line of code above applies the same condition to all the rows. I can provide further explanations if necessary.

Upvotes: 1

Views: 2483

Answers (2)

If @jezrael solution returns out: (TypeError:_translate() missing 2 required positional arguments: 'sparse_index' and 'sparse_cols')

Downgrading pandas to version 1.2.4 can be a temporary solution...

# Uninstall any pandas library installed:
pip uninstall pandas

# After uninstalling pandas, install pandas==1.2.4
pip install pandas==1.2.4

Then you can try to create DataFrame filled by background-colors by conditions, e.g. from your list in Styler.apply, as @jezrael solution.

Alternative solution using df.loc instead of np.where:

checkList =  [True, True, False]

def highlight(x):
   c1 = 'background-color: red'
   c2 = 'background-color: green'

   # If necessary pass condition 
   checkList =  x['Balance'] <= 300
   checkList2 = x['Balance'] > 300
   
  # Empty DataFrame of styles
  df1 = pd.DataFrame(x, index=x.index, columns=x.columns)

  #set Balance column by condition in checkList (using df1.loc instead of np.where)
  df1.loc[checkList, 'Balance'] = c1
  df1.loc[chekcList2, 'Balance'] = c2

  # Return styled dataset
  return df1

# To apply highlight styler:
df.style.apply(highlight, axis=None)

Upvotes: 0

jezrael
jezrael

Reputation: 862431

You can create DataFrame filled by background-colors by conditions, e.g. from your list in Styler.apply:

checkList =  [True, True, False]

def highlight(x):
    c1 = 'background-color: red'
    c2 = 'background-color: green' 

    #if necessary pass condition
    #checkList = x['Balance'] < 300
    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set Balance column by condition in list (necessary same length like df)
    df1['Balance'] = np.where(checkList, c1, c2)
    return df1


df.style.apply(highlight, axis=None)

Upvotes: 2

Related Questions