qtconsole not rendering pandas dataframes as html

2019-03-13 11:48发布

问题:

I recently upgraded my version of pandas. I have the latest stable version installed now:

pd.__version__
Out[5]: '0.10.1'

prior to this upgrade, this is how dataframes were displayed in the qtconsole shell (this isn't my screenshot but simply one i found on the web).

The latest version of pandas also uses a different approach to setting the display options.

Rather than using pd.set_printoptions, pandas wants you to use the set_option configs like this:

pd.set_option('display.notebook_repr_html', True)

After upgrading my pandas version, qtconsole no longer renders dataframes as html tables.

An example:

import numpy as np
import pandas as pd

pd.set_option('display.notebook_repr_html', True)
pd.set_option('display.expand_frame_repr', True)
pd.set_option('display.precision', 3)
pd.set_option('display.line_width', 100)
pd.set_option('display.max_rows', 50)
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_colwidth', 15)

When I create a DataFrame...

f = lambda x: x*np.random.rand()
data = {"a": pd.Series(np.arange(10) ** 2 ),
        "b": pd.Series(map(f, np.ones(10))) }
df = pd.DataFrame(data)
df

This is what I see in the qtconsole shell:

Out[4]: 
    a     b
0   0  0.15
1   1  0.74
2   4  0.81
3   9  0.94
4  16  0.40
5  25  0.03
6  36  0.40
7  49  0.43
8  64  0.56
9  81  0.14

You can check how your display configs are currently set:

opts = ["max_columns", 
        "max_rows", 
        "line_width", 
        "max_colwidth", 
        "notebook_repr_html", 
        "pprint_nest_depth", 
        "expand_frame_repr" ]

for opt in opts:
    print opt, pd.get_option(opt)

Out[5]
max_columns 10
max_rows 50
line_width 100
max_colwidth 15
notebook_repr_html True
pprint_nest_depth 3
expand_frame_repr True

What am I missing in order to render the prettified html tables in qtconsole?

回答1:

As far as I know, the notebook_repr_html option only applies to the actual IPython Notebook and not the QTConsole.

In the QTConsole, you can do:

from IPython.display import HTML
import numpy as np
import pandas

df = pandas.DataFrame(np.random.normal(size=(75,5)))
HTML(df.to_html())

One problem you might encounter is if the HTML is too long for your QTConsole's buffer. In that case nothing will show up, in my experience.