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

iPython - Display full dataframe in new tab

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

当我想控制一个dataframe时,我通常将它导出为.csv,然后在Excel中打开。
我正在寻找一种更快的方法,但我不愿意将全帧显示到我的笔记本中,因为这会使它变得不可读。

由于一帧的正常输出是 HTML table,我想知道我们如何才能在 Notebook 之外的其他地方显示这个 table。

我找到了答案:使用 .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)

您可以使用 javascriptipython.display 打开一个新的 window 并在其中显示整个数据框。优点是您可以多次执行此操作,而无需在其间创建实际的 HTML 文件或重新加载 window。 自动更新打开的 window 并模拟 R/RStudio:

中的 View() 函数
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)