Reputation: 793
Considering the following two toy dataframes,
import pandas as pd
df1 = pd.DataFrame({'A':[0,123,321], 'B':[0,1543,432], 'C': [10,20,30]})
df2 = pd.DataFrame({'A':[0,10,20], 'B':[50,43,42], 'C': [15,25,45]})
how can I set a given style for specific cells of df1
in a jupyter notebook, based on df2
(of the same size), by using the subset
parameter?
For example, set these CSS values only if the corresponding df2
cell value is above 40.
df1.style.set_properties(**{'color': 'red', 'font-size': '10px'})
#,subset= ???)
currently returning:
EDIT1: addressing a comment, the desired output should be the table above but the only cells with red font and 10 px font-size should be B0, B1, B2, and C2
Upvotes: 0
Views: 36
Reputation: 150735
Try style.apply
with a reference dataframe and axis=None
.
def highlight(df, ref_df=None, thresh=None):
return pd.DataFrame(np.where(ref_df>thresh, 'color:red; font-size:13pt', ''),
index=df.index, columns=df.columns)
df1.style.apply(highlight, ref_df=df2, thresh=40, axis=None)
Output:
Upvotes: 1