pandas DataFrame to_html 中的粗体列

Fin*_*und 5 html python pandas

我试图用一个粗体列返回 df.to_html() 。我只尝试过

df = pd.DataFrame({'important_column': [1,2,3,4], 
                   'dummy_column': [5,6,7,8]})

def some_function()
      df.apply(lambda x: '<b>' + str(df['important_column']) + '</b>', axis=1)
      return [df.to_html()]
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用。有人知道实用的解决方案吗?

Jon*_*nts 9

您可以使用df.style.set_propertiesand then为.render()普通表输出添加.to_html()适当的style元素前缀。(请注意,这不会将您的文本元素物理包装在您想要的<b><strong>或任何您想要的标签内,而是纯粹为这些单元格提供样式 - 这可能是也可能不是您想要的,具体取决于用例)

html = df.style.set_properties(
    subset=['important_column'], 
    **{'font-weight': 'bold'}
).render()
Run Code Online (Sandbox Code Playgroud)

(jupyter笔记本中显示的示例)

在此输入图像描述


jez*_*ael 2

您忘记分配输出,但更快的矢量化解决方案是将列转换为字符串并添加带有 noapply和string 的f字符串:

\n\n
def some_function():\n\n    df[\'important_column\'] = [f\'<b>{x}</b>\' for x in df[\'important_column\']]\n    #alternative1 \n    df[\'important_column\'] =  \'<b>\' + df[\'important_column\'].astype(str) + \'</b>\'\n    #alternative2\n    #df[\'important_column\'] = df[\'important_column\'].apply(lambda x: \'<b>\' + str(x) + \'</b>\')\n    #alternative3, thanks @Jon Clements\n    #df[\'important_column\'] = df[\'important_column\'].apply(\'<b>{}</b>?\'.format)\n    return df.to_html()\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑:

\n\n
df[\'important_column\'] = [f\'<b>{x}</b>\' for x in df[\'important_column\']]\nprint (df.to_html(escape=False))\n<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>important_column</th>\n      <th>dummy_column</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td><b>1</b></td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td><b>2</b></td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td><b>3</b></td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td><b>4</b></td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n
Run Code Online (Sandbox Code Playgroud)\n\n

时间

\n\n
df = pd.DataFrame({\'important_column\': [1,2,3,4], \n                   \'dummy_column\': [5,6,7,8]})\n\ndf = pd.concat([df] * 10000, ignore_index=True)\n\nIn [213]: %timeit df[\'important_column\'] = [f\'<b>{x}</b>\' for x in df[\'important_column\']]\n74 ms \xc2\xb1 22.2 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 100 loops each)\n\nIn [214]: %timeit df[\'important_column\'] = df[\'important_column\'].apply(lambda x: \'<b>\' + str(x) + \'</b>\')\n150 ms \xc2\xb1 7.75 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\n\nIn [216]: %timeit df[\'important_column\'].apply(\'<b>{}</b>?\'.format)\n133 ms \xc2\xb1 238 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\n\nIn [217]: %timeit \'<b>\' + df[\'important_column\'].astype(str) + \'</b>\'\n266 ms \xc2\xb1 1.21 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 1 loop each)\n
Run Code Online (Sandbox Code Playgroud)\n