3

I want to style my table with Pandas with the following example from this site: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

Now, the problem is, that I use Pycharm. So after executed my code, the table creates its own colors based on the values. But this is not what I am looking for. Does anyone have the same problem with 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)
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Derick Kolln
  • 633
  • 7
  • 17
  • 1
    I believe you have the same problem as I just posted in this question https://stackoverflow.com/questions/58505444/is-it-possible-to-display-pandas-styles-in-the-ipython-console. Let's see if we find out something. – divingTobi Oct 22 '19 at 13:56

3 Answers3

3

I believe that the pandas stylers produce HTML output, which require a HTML frontend like jupyter to display them. PyCharm is "just" a console and does not render HTML. Therefore you will get a <pandas.io.formats.tyler.Styler...> response.

To verify that the your code works, you can type s.render() and it should print the HTML which would produce the proper formatting.

See also here.

divingTobi
  • 2,044
  • 10
  • 25
0

Add to your code:

with open('test.html', 'w') as f:
    print(s.to_html(), file=f)

and in your project folder will appear file "test.html", where will be your table

Shkum
  • 501
  • 1
  • 5
  • 9
0

.render() has changed to .to_html() but according to that answer it is not possible to render HTML on PyCharm.

Begoodpy
  • 1,018
  • 3
  • 8
  • 20