将 pandas 数据框显示到另一个选项卡中

display pandas dataframe into another tab

我正在尝试在另一个 "Output View" 选项卡中显示我的 pandas 数据框,如此 iamge 中所示...

https://github.com/quantopian/qgrid/blob/master/docs/images/events_api.gif

我可以使用以下命令安装和试用 qgrid 的基本功能。但无法获得如上所示的确切视图。

!pip install qgrid
!jupyter nbextension enable --py --sys-prefix qgrid
!jupyter nbextension enable --py --sys-prefix  widgetsnbextension

import qgrid
import pandas as pd
df = pd.read_csv('some.csv')

qgrid_widget = qgrid.show_grid(df, show_toolbar=True)
qgrid_widget

qgrid_widget.get_changed_df()

由于这个问题有赏金,我不能将其标记为重复。要在另一个选项卡中显示 dataframe,您可以使用一点 JavaScript。根据 post,这里是将在另一个标签中显示数据框的代码:

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(my_dataframe)

这应该会在另一个选项卡中打开数据框。

这些命令应该有效:

预安装:

1. Assume you have conda environment called "myenv"
2. Assume you have jupyter-lab installed in that environment

安装新的Conda环境

source activate myenv
pip install qgrid
jupyter labextension install qgrid
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter nbextension enable --py --sys-prefix qgrid
jupyter nbextension enable --py --sys-prefix widgetsnbextension

加载conda环境

source activate myenv
jupyter-lab
create a notebook under environment myenv

-------------------- 这些只是安装前的程序------
-------------------- 下面是你如何在 jupyter lab 中使用 qgrid ----------

# Lets say you have a pandas dataframe `df`
qgrid_widget = qgrid.show_grid(df.head(), show_toolbar=True)
qgrid_widget
qgrid_widget.get_changed_df()

# right click on this cell
# click Create New View for Output # it will create new tab
# You will see the new window of dataframe

确认

我刚刚按照新环境的程序进行操作,并且有效。