使用 Pandas (df.style.applymap) 在 Pycharm 中设置 table 的样式?

Styling of a table in Pycharm using Pandas (df.style.applymap)?

我想用 Pandas 来设置我的 table 的样式,以下示例来自本网站: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

现在,问题是我使用 Pycharm。所以在执行我的代码后,table 根据值创建它自己的颜色。但这不是我要找的。有人对 Pycharm 有同样的问题吗?

        import pandas as pd
        import numpy as np

        np.random.seed(24)
        df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
        df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
                       axis=1)
        df.iloc[0, 2] = np.nan
    def color_negative_red(val):
        """
        Takes a scalar and returns a string with
        the css property `'color: red'` for negative
        strings, black otherwise.
        """
        color = 'red' if val < 0 else 'black'
        return 'color: %s' % color
s = df.style.applymap(color_negative_red)

我相信 pandas 样式器会产生 HTML 输出,这需要像 jupyter 这样的 HTML 前端来显示它们。 PyCharm 是 "just" 控制台,不呈现 HTML。因此,您将收到 <pandas.io.formats.tyler.Styler...> 回复。

要验证您的代码是否有效,您可以键入 s.render(),它应该打印 HTML,这将产生正确的格式。

另见