问题
I am trying to output a pandas dataframe with colored cells to the console using the pandas Styler feature. When I run code examples in Spyder 3.2.6 running python 2.7, instead of seeing the dataframe as expected, the console outputs "...pandas.core.style.Styler at 0x1d0d7cf8>..."....this is what I would expect see with high-dimensional objects that ie need to be subset in order to view properly in the console
I've also tried calling .render() on the dataframe but this outputs a lot of unicode (?) text to console which is useless
I've also tried various examples from the Styler user guide with the same results (https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html) and also the example from this post: Coloring Cells in Pandas
def _color_red_or_green(val):
color = 'red' if val < 0 else 'green'
return 'color: %s' % color
s = df.style.applymap(_color_red_or_green)
s
# and
s.render()
# example from pandas page:
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
df.style
I expect to see my dataframe output with colored cell but only see:
<pandas.core.style.Styler at 0x1d0d7cf8>
what am I missing? perhaps my Spyder isn't set up to read unicode properly or something?
回答1:
(Spyder maintainer here) Pandas styler only works in the Jupyter notebook for now, not in Spyder.
Note: We have plans to support this in Spyder 5, to be released in 2020.
来源:https://stackoverflow.com/questions/58116600/pandas-styler-not-printing-dataframe-to-spyders-console-as-expected