从pandas样式化html表格

时间:2018-08-28 09:24:45

标签: python-3.x pandas dataframe

我有一个长数据框,想要发送给Gmail用户。我使用的方法是pd.to_html()。尽管我已经取得了成功,但该表确实很长而且可读性很差。例如,如果行和列太多(是一个多列级别的表),索引和列的名称就会消失,您将不再能够知道每列的来源。此外,我错过了根据所需内容进一步过滤表格的可能性。

我已经看到类似http://tablefilter.free.fr的内容要添加到您的html表中,但是我不知道如何使用我的脚本来执行此操作,因为它可能会显示在gmail帐户而不是可视化浏览器(也许解决方案是发送html文件而不是嵌入表格。

此外,如果我向下滚动或横向滚动,我仍然希望将索引和列设置为static。

如何实现?

编辑:我已经找到了这个线程,它或多或少地做了我想要的。不过,我仍然不知道如何在pd.to_html()上实现此功能 Table header to stay fixed at the top when user scrolls it out of view with jQuery

编辑2:我完成了finalframe.to_html(),这是字符串的一小部分,其中包括标题和第一行数据。

'<table border="1" class="dataframe">\n  <thead>\n    <tr>\n      <th>generales</th>\n      <th>fecha</th>\n      <th>competicion</th>\n      <th>local</th>\n      <th>visitante</th>\n      <th>tracker</th>\n      <th colspan="44" halign="left">local</th>\n      <th colspan="44" halign="left">visitante</th>\n      <th>Predicciones</th>\n    </tr>\n    <tr>\n      <th>h/a o totales</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th colspan="22" halign="left">h/a</th>\n      <th colspan="22" halign="left">totales</th>\n      <th colspan="22" halign="left">h/a</th>\n      <th colspan="22" halign="left">totales</th>\n      <th></th>\n    </tr>\n    <tr>\n      <th>tipo de dato</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th colspan="14" halign="left">medias</th>\n      <th colspan="8" halign="left">porcentajes</th>\n      <th colspan="14" halign="left">medias</th>\n      <th colspan="8" halign="left">porcentajes</th>\n      <th colspan="14" halign="left">medias</th>\n      <th colspan="8" halign="left">porcentajes</th>\n      <th colspan="14" halign="left">medias</th>\n      <th colspan="8" halign="left">porcentajes</th>\n      <th></th>\n    </tr>\n    <tr>\n      <th>dato</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>partidos</th>\n      <th>puntos por partido</th>\n      <th>Goles a favor</th>\n      <th>desv GAF</th>\n      <th>Goles en contra</th>\n      <th>desv GEC</th>\n      <th>Goles HT favor</th>\n      <th>desv GHTF</th>\n      <th>Goles HT contra</th>\n      <th>desv GHTC</th>\n      <th>Corner a favor</th>\n      <th>desv CAF</th>\n      <th>Corner en contra</th>\n      <th>desv CEC</th>\n      <th>o2,5</th>\n      <th>o3,5</th>\n      <th>Gol al HT a favor</th>\n      <th>Gol al HT en contra</th>\n      <th>Gol al HT</th>\n      <th>fts</th>\n      <th>cs</th>\n      <th>bts</th>\n      <th>partidos</th>\n      <th>puntos por partido</th>\n      <th>Goles a favor</th>\n      <th>desv GAF</th>\n      <th>Goles en contra</th>\n      <th>desv GEC</th>\n      <th>Goles HT favor</th>\n      <th>desv GHTF</th>\n      <th>Goles HT contra</th>\n      <th>desv GHTC</th>\n      <th>Corner a favor</th>\n      <th>desv CAF</th>\n      <th>Corner en contra</th>\n      <th>desv CEC</th>\n      <th>o2,5</th>\n      <th>o3,5</th>\n      <th>Gol al HT a favor</th>\n      <th>Gol al HT en contra</th>\n      <th>Gol al HT</th>\n      <th>fts</th>\n      <th>cs</th>\n      <th>bts</th>\n      <th>partidos</th>\n      <th>puntos por partido</th>\n      <th>Goles a favor</th>\n      <th>desv GAF</th>\n      <th>Goles en contra</th>\n      <th>desv GEC</th>\n      <th>Goles HT favor</th>\n      <th>desv GHTF</th>\n      <th>Goles HT contra</th>\n      <th>desv GHTC</th>\n      <th>Corner a favor</th>\n      <th>desv CAF</th>\n      <th>Corner en contra</th>\n      <th>desv CEC</th>\n      <th>o2,5</th>\n      <th>o3,5</th>\n      <th>Gol al HT a favor</th>\n      <th>Gol al HT en contra</th>\n      <th>Gol al HT</th>\n      <th>fts</th>\n      <th>cs</th>\n      <th>bts</th>\n      <th>partidos</th>\n      <th>puntos por partido</th>\n      <th>Goles a favor</th>\n      <th>desv GAF</th>\n      <th>Goles en contra</th>\n      <th>desv GEC</th>\n      <th>Goles HT favor</th>\n      <th>desv GHTF</th>\n      <th>Goles HT contra</th>\n      <th>desv GHTC</th>\n      <th>Corner a favor</th>\n      <th>desv CAF</th>\n      <th>Corner en contra</th>\n      <th>desv CEC</th>\n      <th>o2,5</th>\n      <th>o3,5</th>\n      <th>Gol al HT a favor</th>\n      <th>Gol al HT en contra</th>\n      <th>Gol al HT</th>\n      <th>fts</th>\n      <th>cs</th>\n      <th>bts</th>\n      <th>Poisson o25</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2018/08/28 10:30</td>\n      <td>ALG U21</td>\n      <td>CS Constantine U21</td>\n      <td>JS Saoura U21</td>\n      <td>1611191</td>\n      <td>1</td>\n      <td>3.000000</td>\n      <td>2.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>2.000000</td>\n      <td>0.000000</td>\n      <td>4.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>1</td>\n      <td>3.000000</td>\n      <td>2.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>2.000000</td>\n      <td>0.000000</td>\n      <td>4.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>1</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>3.000000</td>\n      <td>0.000000</td>\n      <td>0.00</td>\n      <td>0.000000</td>\n      <td>2.000000</td>\n      <td>0.000000</td>\n      <td>6.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>2</td>\n      <td>1.500000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.500000</td>\n      <td>1.500000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>4.500000</td>\n      <td>1.500000</td>\n      <td>2.500000</td>\n      <td>1.500000</td>\n      <td>0.500000</td>\n      <td>0.000000</td>\n      <td>0.500000</td>\n      <td>0.500000</td>\n      <td>1.000000</td>\n      <td>0.50</td>\n      <td>0.500000</td>\n      <td>0.000000</td>\n      <td>0.576810</td>\n    </tr>\n'

0 个答案:

没有答案