Is possible to rewritte this function
def highlight_cells(x):
df = x.copy()
df.loc[:,:] = 'background-color: grey'
df.iloc[1:3, 1:3] = 'background-color: steelblue'
return df
df.style.apply(highlight_cells, axis=None)
so that the execution of the function below would have the same effect?
def highlight_cells(x, indicates):
df = x.copy()
df.loc[:,:] = 'background-color: grey'
df.iloc[indicates] = 'background-color: steelblue'
return df
df.style.apply(highlight_cells, axis=None, indicates=[1:3, 1:3])
I would like to pass indicates e.g. array as an argument for further operation in a function
CodePudding user response:
You can use:
def highlight_cells(x, idx):
df = x.copy()
df.loc[:,:] = 'background-color: grey'
df.iloc[idx[0]:idx[1], idx[2]:idx[3]] = 'background-color: steelblue'
print(df)
Then you can pass indicates
array as indicates=[1,3,1,3]
CodePudding user response:
An alternative version of the solution using pd.IndexSlice
https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Finer-Control-with-Slicing
idx = pd.IndexSlice
slice_ = idx[idx[1:2, 1:2]]
def highlight_cells(x):
df = x.copy()
df.loc[:,:] = 'background-color: steelblue'
return df
df.style.apply(highlight_cells, axis=None, subset=slice_)