1
 **testdict** =  {'Dict_1': {'Start Time': '06-10-2020 13:08:58', 'Test Type': 'ERP_To_DBO','CountMatch Result': 'Failed', 'End Time': '06-10-2020 13:09:16'},

'Dict_2': {'Start Time': '06-10-2020 13:09:21', 'Test Type': 'ERP_To_DBO','CountMatch Result': 'Failed', 'End Time': '06-10-2020 13:11:53'}, 

'Dict_2': {'Start Time': '06-10-2020 13:11:59', 'Test Type': 'ERP_To_DBO', 'CountMatch Result': 'Failed', 'End Time': '06-10-2020 13:13:19'}}

I want to highlight value 'failed' with red and 'Passed' with green.

df = pd.DataFrame(data=testdict)

def color_negative_red(val):
   color = 'red' if val == 'Failed' else 'black'
     return 'color: %s' % color

 df = df.style.apply(color_mapper)
 df.transpose().to_html(filename)

I have tried many things listed in https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html but didn't got any success.

once the style has applied i have to transpose the entire DF and render a DataFrame as an HTML table.

Techie
  • 31
  • 2

2 Answers2

1

Try operating on the entire column at a time inside your function:

def color_mapper(values):
    mapping = {'Failed': 'red', 'Passed': 'green'}
    return [
        'background-color: {}'.format(mapping.get(v, 'black') for v in values]

df.style.apply(color_mapper)
cs95
  • 379,657
  • 97
  • 704
  • 746
0

Your code isn't running because you should use applymap() when styling and also because return is inside a condition, so when value enter in one of the two first conditionals, the function will never return. Now, when you are using applymap() you are applying a function to each value of your dataframe, so you can era the for key, value in value():. Try change code like this:

def color_failed_red(value): 
    if value =='Failed':
      color = 'red'
    elif value == 'Passed':
      color = 'green'
    else:
      color = 'black'
    return 'color: %s' % color
df.style.applymap(color_failed_red).render()
df.to_html('C:\\Users\\Desktop\\testresult.html')

You can see the explanation here. And you also should know that Pandas styler only works in the Jupyter notebook for now, according to link, so when yo do:

print(df.style.applymap(color_failed_red))
>>><pandas.io.formats.style.Styler object at 0x7f91368b4b80>

If anything, you'll get the expected result if you use applymap() and edit the code as I showed you before. But, I tried another solution, more exemplifying script, so you can see how the color changed. Nonetheless, I didn't get the perfect result. But in case you maybe prefer this solution and want to work in it, this is what I've been trying :

def color_failed_red(x):
  color=''
  if x =='Failed':
    color = '\033[91m'  #red
  elif x == 'Passed':
    color = '\033[92m'  #green
  else:
    color=''
  return color+x
df1=df.applymap(color_failed_red)
print(df1)

Output:

approach to solution I got this way of print colored from this link.

MrNobody33
  • 6,413
  • 7
  • 19
  • this throwing error 'return color+x' Exception has occurred: TypeError can only concatenate str (not "int") to str – Techie Jun 10 '20 at 11:08
  • Tha'ts weird, since all the values in df are strings. I've test it in cmd, online compiler, spyder and jupyter, and I got no errors. Where are you running the script? – MrNobody33 Jun 10 '20 at 11:52
  • Still weird, can you check if `'\033[91m'` is string type? And can you try something like `return color+str(x)`? – MrNobody33 Jun 10 '20 at 12:31