1

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)

enter image description here

Has anybody an idea how to solve it by using style.apply or by hiding the metadata column?

1 Answer 1

2

I think new column is not necessary, need DataFrame of styles only by original DataFrame columns and index and set rows by condition with mask:

def highlight(x):
    c1 = 'background-color: green'
    c2 = 'background-color: red' 

    df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
    df1 = df1.mask(df['sum_of_points'].map(should_colored).ffill(), c1)
    #print (df1)
    return df1

df.style.apply(highlight, axis=None)

Sample:

df = pd.DataFrame({'sum_of_points':[0,1,2,1,2,3,4,1,2,2,4,5,0,1,2],
                   'A':range(15)})

print (df)
     A  sum_of_points
0    0              0
1    1              1
2    2              2
3    3              1
4    4              2
5    5              3
6    6              4
7    7              1
8    8              2
9    9              2
10  10              4
11  11              5
12  12              0
13  13              1
14  14              2

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 highlight(x):
    c1 = 'background-color: green'
    c2 = 'background-color: red' 

    df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
    df1 = df1.mask(x['sum_of_points'].map(should_colored).ffill(), c1)
    return df1

df.style.apply(highlight, axis=None)

pic

Sign up to request clarification or add additional context in comments.

7 Comments

First thanks @jezrael. I brought it to run in the console but not in Flask. Maybe this is the reason why I have so much problems. def initialize(): df = pd.DataFrame({'sum_of_points':[0,1,2,1,2,3,4,1,2,2,4,4,0,1,2],'A':range(15)}) df.style.apply(highlight, axis=None How must it look if I'm not using a global dataframe?
The data are coming from a database which first will be open if the URL will be invoked. At the moment I have the undefined name df error and I'm sure if can do this globally and when.
@Clueless - I have no experience with flask, so I have no idea :(
I put it now into a one-liner and the method has access to the local dataframe df.style.apply(lambda x: pd.DataFrame('background-color: red', index=x.index, columns=x.columns).mask(df['sum_of_points'].map(should_colored).ffill(), 'background-color: green'), axis=None) Even I wonder how this dataframe stuff should work without a global variables.
It works even with the highlight method. I had to change df to x df1 = df1.mask(x['sum_of_points'].map(should_colored).ffill(), c1)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.