Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

python - How do I use pandas DataFrame.style?

I am trying to format my dataframe in HTML using the newly included 'style' in Pandas 0.19. I am trying to replicate the same things using the exact same scripts but I get a Styler object.

>>> df
              Security       Price  Change Percentage  ChgNetValue  LastClose
10     Japan Five-Year   -0.207000          -4.020000    -0.008000  -0.199000
13        U.S. 10-Year   97.406250        5274.434452    95.593850   1.812500
20        U.S. 30-Year   93.156250        3481.253980    90.555031   2.601562
23      U.S. 3M T-Bill    0.355000          -2.068966    -0.007500   0.362500
31      U.S. Five-Year  100.015625        7803.952347    98.750237   1.265625
36  Eurozone Five-Year   -0.423000          -3.171000    -0.013000  -0.410000
43       U.S. Two-Year   99.898438       12271.036276    99.090919  25.875000
44      U.K. Five-Year    0.529000          -3.114000    -0.017000   0.546000    
>>> df.style
    <pandas.formats.style.Styler object at 0x0000000008133F28>
>>> s = df.style.applymap(color_negative_red)
>>> s
    <pandas.formats.style.Styler object at 0x00000000035116A0>

I have the exact same function, color_negative_red, as in the documentation. I am doing this in Eclipse, not IPython.

What I want is to use to_html() and apply conditional formatting to each number in the dataframe. Is there a better way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the note below Out [2] here. Styler objects define a _repr_html_ which renders the HTML in the notebook.

To get the actual HTML string, you call styler.render().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...