I'm trying to style a simple multiplication table. I have the following rule, and if a cell doesn't satisfy it, I must colorize it in a different color.
Example 1: Given numbers 10 and 11, the rule is broken since -798010 != 110, so the color is red:
- Combine
(100-(100-10)-(100-11)) = -79and(100-10)*(100-11) = 8010as strings: -798010 - Multiply
10*11: 110
Example 2: Given numbers 10 and 99, the rule is satisfied since 990 == 990, so the color is green.
- Combine
(100-(100-10)-(100-99)) = 9and(100-10)*(100-99) = 90as strings: 990 - Multiply
10*90: 990
Currently I'm checking if numbers are in the rule or not. Either way I colorize cells, all I get is all cells in one color:
import pandas as pd
l = []
for i in range(10, 100):
for j in range(10, 100):
l.append(f'{i}*{j}')
result = [l[idx:idx+90] for idx in range(0, len(l), 90)]
df = pd.DataFrame(result)
def style_dataframe(s):
for row_index, row in df.iterrows():
for col_name, cell in row.items():
if int(cell[:2])*int(cell[3:5]) == int(str(100 - ((100 - (int(cell[:2])))-(100-(int(cell[3:5])))))+str((100-(int(cell[:2])))*int(int(cell[3:5])))):
return 'background-color: green'
elif int(cell[:2])*int(cell[3:5]) != int(str(100 - ((100 - (int(cell[:2])))-(100-(int(cell[3:5])))))+str((100-(int(cell[:2])))*int(int(cell[3:5])))):
return 'background-color: red'
s = df.style.applymap(style_dataframe)
