pandas 数据框to_html:突出显示表格行 [英] Pandas Dataframes to_html: Highlighting table rows
问题描述
我使用pandas to_html函数创建表格,我希望能够突出显示输出表格的底部行,该表格的长度是可变的。我没有任何HTML的真正经验可言,我在网上发现的只有这个
< table border =1>
< tr style =background-color:#FF0000>
第Month< / th>
< th>储蓄< / th>
< / tr>
< tr>
< td> 1月< / td>
< td> $ 100< / td>
< / tr>
< / table>
所以我知道最后一行必须有< tr style = background-color:#FF0000>
(或者我想要的任何颜色),而不仅仅是< tr>
我真的知道该怎么做才能让我的表发生这种情况,我不认为我可以用to_html函数本身来做,但是我怎么能在表创建完成之后呢?
任何帮助表示赞赏。
$('table tbody tr>解决方案
').filter(':last').css('background-color','#FF0000')
更新版本的熊猫在表格html中添加一个类 dataframe
,这样你就可以用下面的方式过滤掉熊猫表:
$('table.dataframe tbody tr')。filter(':last')。css('background-color','#FF0000')
但是,如果您愿意,您可以添加自己的类:
df.to_html( class ='my_class')
甚至可以是多个:
df.to_html(classes = ['my_class','my_other_class'])
如果您使用的是IPython Notebook,那么这里是一个完整的工作示例:
In [1] :import numpy as np
将pandas作为pd
从IPython.display导入HTML,Javascript
[2]:df = pd.DataFrame({'a':np.arange(10 ),'b':np.random.randn(10)})
In [3]:HTML(df.to_html(classes ='my_class'))
In [4]:Javascript(' ''$('。my_class tbody tr')。filter(':last')
.css('background-color','#FF0000');
''')
或者您甚至可以使用纯CSS:
In [5]:HTML('''
< style>
.df tbody tr:last-child { background-color:#FF0000;}
< / style>
'''+ df.to_html(classes ='df'))
可能性是无限的:)
编辑:创建一个html文件
import numpy as np
将pandas导入为pd
HEADER ='''
< html>
< head>
< style>
.df tbody tr:last-child {background-color:#FF0000; }
< / style>
< / head>
< body>
'''
FOOTER ='''
< / body>
< / html>
'''
df = pd.DataFrame({'a':np.arange(10),'b':np.random.randn(10)})
打开('test.html','w')为f:
f.write(HEADER)
f.write(df.to_html(classes ='df'))
f.write(FOOTER)
I'm creating tables using the pandas to_html function, and I'd like to be able to highlight the bottom row of the outputted table, which is of variable length. I don't have any real experience of html to speak of, and all I found online was this
<table border="1">
<tr style="background-color:#FF0000">
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
So I know that the final row must have <tr style=""background-color:#FF0000">
(or whatever colour I want) rather than just <tr>
, but what I don't really know how to do is get this to occur with the tables I'm making. I don't think I can do it with the to_html function itself, but how can I do it after the table has been created?
Any help is appreciated.
You can do it in javascript using jQuery:
$('table tbody tr').filter(':last').css('background-color', '#FF0000')
Also newer versions of pandas add a class dataframe
to the table html so you can filter out just the pandas tables using:
$('table.dataframe tbody tr').filter(':last').css('background-color', '#FF0000')
But you can add your own classes if you want:
df.to_html(classes='my_class')
Or even multiple:
df.to_html(classes=['my_class', 'my_other_class'])
If you are using the IPython Notebook here is the full working example:
In [1]: import numpy as np
import pandas as pd
from IPython.display import HTML, Javascript
In [2]: df = pd.DataFrame({'a': np.arange(10), 'b': np.random.randn(10)})
In [3]: HTML(df.to_html(classes='my_class'))
In [4]: Javascript('''$('.my_class tbody tr').filter(':last')
.css('background-color', '#FF0000');
''')
Or you can even use plain CSS:
In [5]: HTML('''
<style>
.df tbody tr:last-child { background-color: #FF0000; }
</style>
''' + df.to_html(classes='df'))
The possibilities are endless :)
Edit: create an html file
import numpy as np
import pandas as pd
HEADER = '''
<html>
<head>
<style>
.df tbody tr:last-child { background-color: #FF0000; }
</style>
</head>
<body>
'''
FOOTER = '''
</body>
</html>
'''
df = pd.DataFrame({'a': np.arange(10), 'b': np.random.randn(10)})
with open('test.html', 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
这篇关于 pandas 数据框to_html:突出显示表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!