代码之家  ›  专栏  ›  技术社区  ›  Finrod Felagund

将数据框中的粗体列转换为html

  •  2
  • Finrod Felagund  · 技术社区  · 6 年前

    我正试着回来数据框到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()]
    

    但似乎不起作用。有人知道一个切实可行的解决办法吗?

    3 回复  |  直到 6 年前
        1
  •  2
  •   jezrael    6 年前

    apply f 串:

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

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

    时间安排 :

    df = pd.DataFrame({'important_column': [1,2,3,4], 
                       'dummy_column': [5,6,7,8]})
    
    df = pd.concat([df] * 10000, ignore_index=True)
    
    In [213]: %timeit df['important_column'] = [f'<b>{x}</b>' for x in df['important_column']]
    74 ms ± 22.2 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
    
    In [214]: %timeit df['important_column'] = df['important_column'].apply(lambda x: '<b>' + str(x) + '</b>')
    150 ms ± 7.75 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    In [216]: %timeit df['important_column'].apply('<b>{}</b>?'.format)
    133 ms ± 238 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    In [217]: %timeit '<b>' + df['important_column'].astype(str) + '</b>'
    266 ms ± 1.21 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
        2
  •  4
  •   Jon Clements    6 年前

    你可以使用 df.style.set_properties 然后 .render() .to_html() 以适当的方式 style 元素。 <b> <strong> 或者任何你想要的标签,但是仅仅为那些单元格提供样式-这可能是你想要的,也可能不是,这取决于用例)

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

    enter image description here

        3
  •  0
  •   Finrod Felagund    6 年前

    是我的错,我没有正确地回答这个问题,我需要数据框到html()在函数的末尾,因为这个html显示在一个web页面上,是用flask创建的,所以上面的工作对我来说不是。

    我找到了一个便宜的工作,适合我的需要:

    def some_function():
        df['important_column'] = '^' + df['important_column'].astype(str) + '+'
        return [df.to_html(classes='greenARTstyle').replace('^', '<b>').replace('+', '</b>')]