熊猫DataFrame表垂直滚动条

mag*_*ord 3 python pandas jupyter-notebook

我有一个大的(垂直)熊猫数据框,我想将其显示为带有滚动条的漂亮表格。我可以显示所有行的表,但是无法显示滚动条。

def data(x):
    strData = strData[['Data1','Data2','Data3']]
    display(strData)
Run Code Online (Sandbox Code Playgroud)

输出:无垂直滚动条

在此处输入图片说明

Duk*_*ing 17

另一个答案对我不起作用 -IPython.OutputArea似乎不再存在(据我所知,至少在 VSCode 中并且基于 IPython 代码)。

我设法使用一种有点老套的解决方案使 DataFrame 可滚动,即为 DataFrame 生成 HTML 表,然后将其放入 a 中div,该解决方案可以使用 CSS 使其可滚动。在这种情况下,我们真正需要做的就是将高度设置为某个像素值。

我们还可以将溢出设置为auto并将宽度设置为fit-content,以便滚动条出现在 DataFrame 的旁边,而不是一直出现在窗口的右侧。

import pandas as pd
from IPython.display import display, HTML

df = pd.DataFrame([(i, i) for i in range (20)])

pd.set_option("display.max_rows", None)

# Puts the scrollbar next to the DataFrame
display(HTML("<div style='height: 200px; overflow: auto; width: fit-content'>" +
             df.style.render() +
             "</div>"))

# Puts the scrollbar on the right side of the window
display(HTML("<div style='height: 200px'>" + df.style.render() + "</div>"))
Run Code Online (Sandbox Code Playgroud)

演示:


小智 7

只需传递数据框并观察魔法即可。

def table(df):
    import plotly.graph_objs as go
    fig = go.Figure(data=[go.Table(
    header=dict(values=list(df.columns),
                align='left'),
    cells=dict(values=[df[i] for i in df.columns],           
                align='left'))
    ])
    return fig
Run Code Online (Sandbox Code Playgroud)

  • 您能否解释一下为什么在这种情况下plotly 是一个很好的库,并且也许可以显示它的屏幕截图? (2认同)

Psi*_*dom 5

不确定这是否是您的意思,但是我想您需要将max_rows选项设置为“无”,以便熊猫不会对显示的行数设置限制:

pd.set_option("display.max_rows", None)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


更新

在此处输入图片说明