相关疑难解决方法(0)

用内联样式而不是CSS将pandas DataFrame转换为html

我正在尝试将HTML格式的pandas DataFrame作为电子邮件的一部分发送。我正在尝试使用类似

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()
Run Code Online (Sandbox Code Playgroud)

然后添加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 …
Run Code Online (Sandbox Code Playgroud)

html python html-email pandas

1
推荐指数
1
解决办法
407
查看次数

标签 统计

html ×1

html-email ×1

pandas ×1

python ×1