3

I have written about 100 lines of code in a Jupyter Notebook cell, and in the middle of the code, I have something like this:

pd.set_option('display.max_rows', 1000)
x = np.random.rand(1000,3)
display(pd.DataFrame(x))

It shows me the entire table, but I was wondering if there is an option in Pandas to limit the vertical size of the output table & add a vertical scroll bar to allow better viewing. I mean, a separate vertical scroll bar than the cell output scroll bar (which I have toggled off by the way).

From what I understand, one option is to use Plotly table visualization as explained in this, but I wanted to avoid that if possible & use a more native solution because with plotly / Dash, the table text is not selectable anymore (Not sure haven't tested it yet though).

vvvvv
  • 25,404
  • 19
  • 49
  • 81
dani
  • 147
  • 2
  • 10
  • 2
    Does this thread help: https://stackoverflow.com/questions/42724327/pandas-dataframe-table-vertical-scrollbars (particularly the updated part of the selected answer). – fhorrobin Feb 11 '22 at 19:04
  • @fhorrobin yes that was very helpful! I added the answer to the question text. – dani Feb 11 '22 at 19:13

2 Answers2

0

Found the solution from Pandas DataFrame Table Vertical Scrollbars. Add this:

from IPython.display import display, HTML

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

This answer was posted as an edit to the question How to display large matrix in pandas with scroll bars in Jupyter Notebook? by the OP dani under CC BY-SA 4.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
0

I created this function that I always copy paste on any of my notebook

def print_df(df, head=10):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):
        display(df.head(head))

and I just need to call print_df(df) to check some sample data

d_frEak
  • 440
  • 3
  • 12