Python 使用内联样式而不是CSS将数据框转换为html

Python 使用内联样式而不是CSS将数据框转换为html,python,html,pandas,html-email,Python,Html,Pandas,Html Email,我正在尝试将熊猫数据框作为HTML作为电子邮件的一部分发送。我试着用类似的东西 import pandas as pd df = pd.DataFrame({1: [1, 2, 3], 2: [4, 5, 6]}) def style_map(x): return 'color: red' if x == 1 else 'color: blue' styled_df = df.style.applymap(style_map).render() 然后将styled_df添加到剩余

我正在尝试将熊猫数据框作为HTML作为电子邮件的一部分发送。我试着用类似的东西

import pandas as pd

df = pd.DataFrame({1: [1, 2, 3], 2: [4, 5, 6]})

def style_map(x):
    return 'color: red' if x == 1 else 'color: blue'

styled_df = df.style.applymap(style_map).render()
然后将
styled_df
添加到剩余的HTML中。但是,此方法使用CSS设置表格样式,输出如下所示:

<style  type="text/css" >
    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row0_col0 {
            color:  red;
        }    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row0_col1 {
            color:  blue;
        }    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row1_col0 {
            color:  blue;
        }    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row1_col1 {
            color:  blue;
        }    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row2_col0 {
            color:  blue;
        }    #T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row2_col1 {
            color:  blue;
        }</style><table id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4" ><thead>    <tr>        <th class="blank level0" ></th>        <th class="col_heading level0 col0" >1</th>        <th class="col_heading level0 col1" >2</th>    </tr></thead><tbody>
                <tr>
                        <th id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4level0_row0" class="row_heading level0 row0" >0</th>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row0_col0" class="data row0 col0" >1</td>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row0_col1" class="data row0 col1" >4</td>
            </tr>
            <tr>
                        <th id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4level0_row1" class="row_heading level0 row1" >1</th>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row1_col0" class="data row1 col0" >2</td>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row1_col1" class="data row1 col1" >5</td>
            </tr>
            <tr>
                        <th id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4level0_row2" class="row_heading level0 row2" >2</th>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row2_col0" class="data row2 col0" >3</td>
                        <td id="T_3627a1a0_4fb7_11e9_9bf9_33657f3526e4row2_col1" class="data row2 col1" >6</td>
            </tr>
    </tbody></table>

#T\U 3627a1a0\U 4fb7\U 11e9\U 9bf9\U 33657F3526E4列{
颜色:红色;
}#T#u 3627a1a0_4fb7_11e9_9bf9_33657f35264;uCOL1{
颜色:蓝色;
}#T#u 3627a1a0_4fb7_11e9_9bf9_33657f3526第1列{
颜色:蓝色;
}#T#u 3627a1a0_4fb7_11e9_9bf9_33657f3526第1列{
颜色:蓝色;
}#T#u 3627a1a0_4fb7_11e9_9bf9_33657f3526第2列{
颜色:蓝色;
}#T#u 3627a1a0_4fb7_11e9_9bf9ťu 33657f3526第2列{
颜色:蓝色;
}                    1        2    
0
1.
4.
1.
2.
5.
2.
3.
6.
在这种情况下,大多数电子邮件客户端都会忽略这些样式


我的问题是:是否有一种(简单的)方法可以将样式信息移动到单个单元格的
style=
属性中?

一种方法是将Pandas自己的样式与Python模块结合起来,将CSS转换为内联样式(参见示例)

例如,使用:

[1]中的
:来自预编译器导入转换
在[2]中:导入熊猫作为pd
...:
…:df=pd.DataFrame({1:[1,2,3],2:[4,5,6]})
...:
…:定义样式_映射(x):
…:如果x==1,则返回“颜色:红色”,否则返回“颜色:蓝色”
...:
…:styled_df=df.style.applymap(style_map.render())
在[4]中:打印(转换(样式化)
1        2    
0
1.
4.
1.
2.
5.
2.
3.
6.
In [1]: from premailer import transform

In [2]: import pandas as pd
   ...:
   ...: df = pd.DataFrame({1: [1, 2, 3], 2: [4, 5, 6]})
   ...:
   ...: def style_map(x):
   ...:     return 'color: red' if x == 1 else 'color: blue'
   ...:
   ...: styled_df = df.style.applymap(style_map).render()

In [4]: print(transform(styled_df))
<html><head></head><body><table id="T_47851dee_4fbf_11e9_9c6a_f5d370129713"><thead>    <tr>        <th class="blank level0"></th>        <th class="col_heading level0 col0">1</th>        <th class="col_heading level0 col1">2</th>    </tr></thead><tbody>
                <tr>
                        <th id="T_47851dee_4fbf_11e9_9c6a_f5d370129713level0_row0" class="row_heading level0 row0">0</th>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row0_col0" class="data row0 col0" style="color:red">1</td>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row0_col1" class="data row0 col1" style="color:blue">4</td>
            </tr>
            <tr>
                        <th id="T_47851dee_4fbf_11e9_9c6a_f5d370129713level0_row1" class="row_heading level0 row1">1</th>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row1_col0" class="data row1 col0" style="color:blue">2</td>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row1_col1" class="data row1 col1" style="color:blue">5</td>
            </tr>
            <tr>
                        <th id="T_47851dee_4fbf_11e9_9c6a_f5d370129713level0_row2" class="row_heading level0 row2">2</th>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row2_col0" class="data row2 col0" style="color:blue">3</td>
                        <td id="T_47851dee_4fbf_11e9_9c6a_f5d370129713row2_col1" class="data row2 col1" style="color:blue">6</td>
            </tr>
    </tbody></table></body></html>