Color rows of pandas dataframe and convert to HTML table

后端 未结 1 1365
后悔当初
后悔当初 2021-01-02 09:53

I am trying to show a pandas dataframe using flask. I was successfully doing so, until I decided to colour some of the dataframe\'s rows. In particular I fail when I apply t

1条回答
  •  太阳男子
    2021-01-02 10:42

    As the error message indicates, you are trying to use the DataFrame.to_html() method on a Styler object, since df.style.apply returns a Styler object and not a DataFrame.

    The docs say you can use the render() method to render the HTML.

    Something like this:

    style1 = df.style.apply(highlight_greaterthan,threshold=1.0,column=['C','B'], axis=1)
    df_html = style1.render()
    

    The output of style1.render() would be:

      
    
    A B C D E
    0 1 1.32921 nan -0.31628 -0.99081
    1 2 -1.07082 -1.43871 0.564417 0.295722
    2 3 -1.6264 0.219565 0.678805 1.88927
    3 4 0.961538 0.104011 -0.481165 0.850229
    4 5 1.45342 1.05774 0.165562 0.515018
    5 6 -1.33694 0.562861 1.39285 -0.063328
    6 7 0.121668 1.2076 -0.00204021 1.6278
    7 8 0.354493 1.03753 -0.385684 0.519818
    8 9 1.68658 -1.32596 1.42898 -2.08935
    9 10 -0.12982 0.631523 -0.586538 0.29072

    0 讨论(0)
提交回复
热议问题