Check the code I have provided, check the part where is m1 and m2, I commented there. I want to add multiple strings,is there simple solutions here?
I know this is not the way to do this, but this is only solution I came up. If you have other solution for doing the same think, please provide.
import pandas as pd
NewDataFrame = pd.DataFrame({'Group':['aaa','bbb','ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh'],
'N':[1,2,3,4,5,6,7,8]})
def highlight_cells(x):
c2 = 'background-color: #00FF00'
c1 = 'background-color: red'
c = 'background-color: white'
m1 = NewDataFrame['Group'].str.contains("aaa", na=False) # here to add more strings ("bbb", "ggg")
m2 = NewDataFrame['Group'].str.contains("ccc", na=False) # here to add additional strings ("fff")
#how to add more then one string, not to add new line for every string?
colordata = pd.DataFrame(c, index=x.index, columns=x.columns)
colordata.loc[m1, 'Group'] = c1
colordata.loc[m2, 'Group'] = c2
return colordata
end = StartingDataFrame.style.apply(highlight_cells,axis=None)
end
CodePudding user response:
Add strings with |
like:
def highlight_cells(x):
c2 = 'background-color: #00FF00'
c1 = 'background-color: red'
c = 'background-color: white'
m1 = NewDataFrame['Group'].str.contains("aaa|bbb|ggg", na=False)
m2 = NewDataFrame['Group'].str.contains("ccc|fff", na=False)
#how to add more then one string, not to add new line for every string?
colordata = pd.DataFrame(c, index=x.index, columns=x.columns)
colordata.loc[m1, 'Group'] = c1
colordata.loc[m2, 'Group'] = c2
return colordata
end = StartingDataFrame.style.apply(highlight_cells,axis=None)