te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Python Pandas highlight row in Dataframe - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Python Pandas highlight row in Dataframe - Stack Overflow

programmeradmin3浏览0评论

I have a mysql query which returns a results table. Such as the below:


What I'm trying to achieve is to highlight the whole row when the last column's value is 'MM'.

I've used in the past a small snippet of JS code which would only work for individual cells, not the whole row.


My code is as such:

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:  '<span class="market_make">{:,.0f}</span>'.format(x) if x = 'MM' else '{:,.0f}'.format(x)

    with open(os.path.join(develop_path,report_file), 'w') as f:
        f.write("<b>Stocks with ex-dividend on next trading day<b/><br/><br/>")
        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 = '''
<style>
.market_make {
    background-color: #FBB117;
}
</style>
<script>
var elements = document.getElementsByClassName('market_make');

for(var i=0; i<elements.length; i++) {
    elements[i].parentElement.setAttribute("bgcolor", "#FBB117");
}
</script>
'''

# run the report
d=generate_report()
print d

The html that pandas creates is partly this:

<b>Stocks with ex-dividend on next trading day<b/><br/><br/><table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>description</th>
      <th>feedcode</th>
      <th>date(exdividenddate)</th>
      <th>dividend</th>
      <th>metadata</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>HHI Div</td>
      <td>HHI</td>
      <td>2015-07-02</td>
      <td>34.793976</td>
      <td>NM</td>
    </tr>
    <tr>
      <td>CCB</td>
      <td>939</td>
      <td>2015-07-02</td>
      <td>0.000000</td>
      <td>MM</td>
    </tr>
    <tr>
      <td>null Combo + Stock - Legs</td>
      <td>FXI-H</td>
      <td>2015-07-02</td>
      <td>35.327749</td>
      <td>NM</td>
    </tr>
    <tr>
      <td>CSE</td>
      <td>1088</td>
      <td>2015-07-02</td>
      <td>0.000000</td>
      <td>MM</td>
    </tr>
    <tr>
      <td>PIN</td>
      <td>1339</td>
      <td>2015-07-02</td>
      <td>0.011800</td>
      <td>NM</td>
    </tr>

Is there a more efficient way apart from JS to do this?

Can I bine it with some CSS maybe?

Any ideas please?

I have a mysql query which returns a results table. Such as the below:


What I'm trying to achieve is to highlight the whole row when the last column's value is 'MM'.

I've used in the past a small snippet of JS code which would only work for individual cells, not the whole row.


My code is as such:

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:  '<span class="market_make">{:,.0f}</span>'.format(x) if x = 'MM' else '{:,.0f}'.format(x)

    with open(os.path.join(develop_path,report_file), 'w') as f:
        f.write("<b>Stocks with ex-dividend on next trading day<b/><br/><br/>")
        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 = '''
<style>
.market_make {
    background-color: #FBB117;
}
</style>
<script>
var elements = document.getElementsByClassName('market_make');

for(var i=0; i<elements.length; i++) {
    elements[i].parentElement.setAttribute("bgcolor", "#FBB117");
}
</script>
'''

# run the report
d=generate_report()
print d

The html that pandas creates is partly this:

<b>Stocks with ex-dividend on next trading day<b/><br/><br/><table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>description</th>
      <th>feedcode</th>
      <th>date(exdividenddate)</th>
      <th>dividend</th>
      <th>metadata</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>HHI Div</td>
      <td>HHI</td>
      <td>2015-07-02</td>
      <td>34.793976</td>
      <td>NM</td>
    </tr>
    <tr>
      <td>CCB</td>
      <td>939</td>
      <td>2015-07-02</td>
      <td>0.000000</td>
      <td>MM</td>
    </tr>
    <tr>
      <td>null Combo + Stock - Legs</td>
      <td>FXI-H</td>
      <td>2015-07-02</td>
      <td>35.327749</td>
      <td>NM</td>
    </tr>
    <tr>
      <td>CSE</td>
      <td>1088</td>
      <td>2015-07-02</td>
      <td>0.000000</td>
      <td>MM</td>
    </tr>
    <tr>
      <td>PIN</td>
      <td>1339</td>
      <td>2015-07-02</td>
      <td>0.011800</td>
      <td>NM</td>
    </tr>

Is there a more efficient way apart from JS to do this?

Can I bine it with some CSS maybe?

Any ideas please?

Share Improve this question edited Jun 30, 2015 at 5:25 Stellar asked Jun 30, 2015 at 3:33 StellarStellar 531 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

This can be done with pd.DataFrame.style.apply and a custom style function!

For example, to highlight the 'MM' rows yellow:

def custom_style(row):

    color = 'white'
    if row.values[-1] == 'MM':
        color = 'yellow'

    return ['background-color: %s' % color]*len(row.values)

df.style.apply(custom_style, axis=1)

Rewrite of what alexg said

color = (df.meta == 'MM').map({True: 'background-color: yellow', False: ''}) df.style.apply(lambda s: color)

I found a related question in the Google group of PyData.

most of what you suggest is certainly possible and es down to a well designed style class IMO

e.g.

df.to_html(...., style = Style('color' : { 'blue' : df>0))

You would probably have to change the selection rules to something like df['metadata'] == 'MM'.

发布评论

评论列表(0)

  1. 暂无评论