【问题标题】:How do i round this values (IPython.display)我如何舍入这个值(IPython.display)
【发布时间】:2021-04-30 09:31:32
【问题描述】:

感谢您的关注。

我想内联显示数据框,我找到了 display_html

我想用圆形显示这个浮点值

这是我的代码

a = [[1.0,2.0,3.0,4.0],[1.0,2.0,3.0,4.0]]
b = [[5.0,6.0,7.0,8.0],[5.0,6.0,7.0,8.0]]
df = pd.DataFrame(a)
df2 = pd.DataFrame(b)

space = "\xa0" * 10
display_html(df.head().style.set_table_attributes("style='display:inline'")._repr_html_()
             + space +df2.head().style.set_table_attributes("style='display:inline'")._repr_html_(), raw=True)

我怎样才能使这个值四舍五入到 1.00 2.00

这是输出enter image description here

【问题讨论】:

    标签: python pandas dataframe ipython


    【解决方案1】:

    你可以使用style.set_precision()来达到想要的效果:

    a = [[1.0,2.0,3.0,4.0],[1.0,2.0,3.0,4.0]]
    b = [[5.0,6.0,7.0,8.0],[5.0,6.0,7.0,8.0]]
    df = pd.DataFrame(a)
    df2 = pd.DataFrame(b)
    
    space = "\xa0" * 10
    display_html(df.head().style.set_precision(2).set_table_attributes("style='display:inline'")._repr_html_()
                 + space +df2.head().style.set_precision(2).set_table_attributes("style='display:inline'")._repr_html_(), raw=True)
    

    【讨论】:

    • 谢谢。我做到了它的工作。但是你怎么能找到这个答案呢?我想知道我的进步
    • 在这种情况下,我的存储库中有一段类似的代码,但一般来说,开始查找的最佳位置是 API 文档,例如 pandas 模块:pandas.pydata.org/pandas-docs/stable/reference/index.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2015-01-16
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多