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

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

由于框架的正常输出是一个 HTML 表格,我想知道我们如何在笔记本之外的其他地方显示这个表格。

请您参考如下方法:

您可以使用 javascript连同ipython.display打开一个新窗口并在其中显示整个数据框。优点是您可以多次执行此操作,而无需在其间创建实际的 HTML 文件或重新加载窗口。 My solution for that question自动更新打开的窗口并模拟 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) 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!