如果颜色基于前一行,我会花时间解决着色表行的问题。
在四点系统中有以下逻辑。如果 0 或 1 点,则行颜色应为红色,如果 3 或 4 点,行颜色应为绿色,如果 2 点,颜色应与前一行相同。
我无法确定数据框中的前一行颜色。我用'temp'列解决了它。不幸的是,此列显示在 HTML 表中。
def should_colored(sum_of_points):
if sum_of_points > 2:
return True
elif sum_of_points < 2:
return False
else:
return np.NaN
def determine_coloring_for_row(results):
tmp = pd.DataFrame(results.values, columns=['color_result'])
tmp['color_result'] = tmp['color_result'].apply(lambda i : should_colored(i))
tmp['color_result'].fillna(method='ffill', inplace=True)
return tmp['color_result'].values
def color_row(row, number_of_columns):
color = 'green' if row['color_result'] else 'red'
return ['background-color: %s' % color] * number_of_columns
df['color_result'] = determine_coloring_for_row(df['sum_of_points'])
df.style.apply(color_row, number_of_columns = len(df.columns), axis=1)
有没有人知道如何通过使用 style.apply 或隐藏元数据列来解决它?