我正在try 设计一个简单的乘法表.我有以下规则,如果一个单元格不满足它,我必须用不同的 colored颜色 给它上色.
给出数字10和11,从-798010开始的rule is broken!=110,所以 colored颜色 是红色:
- 将
(100-(100-10)-(100-11)) = -79
和(100-10)*(100-11) = 8010
组合为字符串:-798010 - 乘
10*11
:110
Example 2:给出数字10和99,从990开始的rule is satisfied==990,所以 colored颜色 是绿色的.
- 将
(100-(100-10)-(100-99)) = 9
和(100-10)*(100-99) = 90
组合为字符串:990 - 乘
10*90
:990
目前我正在判断规则中是否包含数字.无论用哪种方法,我得到的都是一种 colored颜色 的所有单元:
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)