title: Python Pandas highlight row in Dataframe
body: <p>I have a mysql query which returns a results table.
  Such as the below:</p>
  
  <p><img src="https://i.stack.imgur.com/L7rpb.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>What I'm trying to achieve is to highlight the whole row when the last column's value is 'MM'.</p>
  
  <p>I've used in the past a small snippet of JS code which would only work for individual cells, not the whole row.</p>
  
  <hr>
  
  <p>My code is as such:</p>
  
  <pre><code>import pandas as pd
  from pandas import DataFrame
  from pandas.tseries.offsets import BDay
  import sys
  import mysql.connector
  import time
  import datetime
  from datetime import date,timedelta
  import os
  
  
  ## testing dates
  currentdate = pd.datetime.today()
  today = currentdate.strftime("%Y-%m-%d")
  yesterday = currentdate - BDay(1)
  yest = yesterday.strftime("%Y-%m-%d")
  #print today
  #print yest
  
  ## where to dump the file
  develop_path = "/var/www/html/exDiv/"
  report_file = "exDivReport." + today + ".html"
  
  ## function to get data and print out
  def get_data_from_hk():
      cnx = mysql.connector.connect(
          user='some_user', 
          password='some_pass',
          host='some_host',
          database='some_database'
      )
  
      cursor = cnx.cursor()
      query = ("SELECT c.description, c.feedcode, date(exdividenddate), dividend, (case when c.metadata like '%3=MM%' then 'MM' when c.metadata is NULL then 'NM' when c.metadata not like '%3=MM%' then 'NM' else c.metadata end) as metadata FROM dividends d join contracts c using(contracttag) where d.kind=0 and d.exdividenddate=getNextTradingDate(1, curdate());")
  
      cursor.execute(query)
      dbdata_hk=cursor.fetchall()
      cnx.close()
      print dbdata_hk
      return dbdata_hk
  
  
  def generate_report():
      report_colnames = ['description', 'feedcode', 'date(exdividenddate)', 'dividend', 'metadata'] 
  
      dbdata_list_hk = get_data_from_hk()
      df = pd.DataFrame(dbdata_list_hk, columns=report_colnames)
  
      ##test: if the last column contains 'MM' then highlight table row
      ##highlight = lambda x:  '&lt;span class="market_make"&gt;{:,.0f}&lt;/span&gt;'.format(x) if x = 'MM' else '{:,.0f}'.format(x)
  
      with open(os.path.join(develop_path,report_file), 'w') as f:
          f.write("&lt;b&gt;Stocks with ex-dividend on next trading day&lt;b/&gt;&lt;br/&gt;&lt;br/&gt;")
          f.write(df.to_html(
              index=False,
              ##the whole row needs to be highlighted
              ##formatters={('description', 'metadata'): highlight}, 
              escape=False,
              justify='center'
          ))
          f.write(style_text)
  
      return df
  
  ## colouring js
  style_text = '''
  &lt;style&gt;
  .market_make {
      background-color: #FBB117;
  }
  &lt;/style&gt;
  &lt;script&gt;
  var elements = document.getElementsByClassName('market_make');
  
  for(var i=0; i&lt;elements.length; i++) {
      elements[i].parentElement.setAttribute("bgcolor", "#FBB117");
  }
  &lt;/script&gt;
  '''
  
  # run the report
  d=generate_report()
  print d
  </code></pre>
  
  <hr>
  
  <p>The html that pandas creates is partly this:</p>
  
  <pre><code>&lt;b&gt;Stocks with ex-dividend on next trading day&lt;b/&gt;&lt;br/&gt;&lt;br/&gt;&lt;table border="1" class="dataframe"&gt;
    &lt;thead&gt;
      &lt;tr style="text-align: right;"&gt;
        &lt;th&gt;description&lt;/th&gt;
        &lt;th&gt;feedcode&lt;/th&gt;
        &lt;th&gt;date(exdividenddate)&lt;/th&gt;
        &lt;th&gt;dividend&lt;/th&gt;
        &lt;th&gt;metadata&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;HHI Div&lt;/td&gt;
        &lt;td&gt;HHI&lt;/td&gt;
        &lt;td&gt;2015-07-02&lt;/td&gt;
        &lt;td&gt;34.793976&lt;/td&gt;
        &lt;td&gt;NM&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;CCB&lt;/td&gt;
        &lt;td&gt;939&lt;/td&gt;
        &lt;td&gt;2015-07-02&lt;/td&gt;
        &lt;td&gt;0.000000&lt;/td&gt;
        &lt;td&gt;MM&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;null Combo + Stock - Legs&lt;/td&gt;
        &lt;td&gt;FXI-H&lt;/td&gt;
        &lt;td&gt;2015-07-02&lt;/td&gt;
        &lt;td&gt;35.327749&lt;/td&gt;
        &lt;td&gt;NM&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;CSE&lt;/td&gt;
        &lt;td&gt;1088&lt;/td&gt;
        &lt;td&gt;2015-07-02&lt;/td&gt;
        &lt;td&gt;0.000000&lt;/td&gt;
        &lt;td&gt;MM&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;PIN&lt;/td&gt;
        &lt;td&gt;1339&lt;/td&gt;
        &lt;td&gt;2015-07-02&lt;/td&gt;
        &lt;td&gt;0.011800&lt;/td&gt;
        &lt;td&gt;NM&lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <hr>
  
  <p>Is there a more efficient way apart from JS to do this?</p>
  
  <p>Can I combine it with some CSS maybe?</p>
  
  <p>Any ideas please?</p>