I spend my time to solve a problem with coloring table row if the color is basing on a previous row.
In a four point system there is following logic. If 0 or 1 point the row color should be red, if 3 or 4 points the row color should be green and if 2 points the color should be as the row before.
I was not able to determine the previous row color in a dataframe. I solved it with a 'temp' column. Unfortunately this column is shown in the HTML table.
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)
Has anybody an idea how to solve it by using style.apply or by hiding the metadata column?

