iPython - 在新标签中显示完整的数据框

Bky*_*kyn 5 ipython pandas jupyter

在Jupyter中,使用Pandas,有没有办法在导航器的新选项卡中显示整个数据框?

当我想控制数据帧时,我通常将其导出为.csv然后在Excel中打开.
我正在寻找一种更快的方式,但我不愿意在我的笔记本中显示完整的框架,因为它使它变得不可读.

由于帧的正常输出是HTML表格,我想知道我们如何能够在除笔记本之外的其他地方显示此表格.

Mar*_*tin 10

您可以javascript一起使用ipython.display以打开一个新窗口并在其中显示整个数据框.优点是您可以多次执行此操作,而无需在其间创建实际的HTML文件或重新加载窗口.我对该问题的解决方案会自动更新打开的窗口,并模拟以下View()功能R/RStudio:

from IPython.display import HTML
def View(df):
    css = """<style>
    table { border-collapse: collapse; border: 3px solid #eee; }
    table tr th:first-child { background-color: #eeeeee; color: #333; font-weight: bold }
    table thead th { background-color: #eee; color: #000; }
    tr, th, td { border: 1px solid #ccc; border-width: 1px 0 0 1px; border-collapse: collapse;
    padding: 3px; font-family: monospace; font-size: 10px }</style>
    """
    s  = '<script type="text/Javascript">'
    s += 'var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));'
    s += 'win.document.body.innerHTML = \'' + (df.to_html() + css).replace("\n",'\\') + '\';'
    s += '</script>'
    return(HTML(s+css))

View(df)
Run Code Online (Sandbox Code Playgroud)


Bky*_*kyn 8

我找到了答案:将框架保存为带.to_html的html页面,然后使用常规python函数webbrowser打开它:

import webbrowser
...
df.to_html("frame.html")
url = "http://localhost:8888/files/notebook/frame.html"
webbrowser.open(url,new=2)
Run Code Online (Sandbox Code Playgroud)