Andrea
Andrea

Reputation: 748

How to prevent vertical scroll bars when outputting Pandas dataframe in Jupyter Notebook?

I have a Jupyter Notebook where when I run:

df.head()

The resulting output is a dataframe with many, many columns therefore resulting in a vertical scrollbar.

This would be fine if I were able to privately share the data as a .html or an .ipynb file, but I need to download it as a static pdf. As a pdf, these data frames are simply getting cut off.

This is a very long notebook with several instances where this happens. What can I do to remedy this?

Thank you

Upvotes: 2

Views: 5180

Answers (2)

Cooper
Cooper

Reputation: 68

Please clarify:

  • Lots of columns shouldn't give you a vertical scroll bar... lots of rows will, but df.head() will only show the first 5 rows by default. for lots of columns df.head() shows the first 10 and last 10 columns, no scroll bar (for me at least).

edit sorry, just realised you must be using:

pd.set_option('display.max_columns', 100)

'I need to download it as a static pdf.'

  • Are you using your browser's print to pdf to get a kind of screengrab of your jupyter notebook? If so, there are better ways of getting a static picture of a dataframe table out of python. df.head() "... is useful for quickly testing if your object has the right type of data in it." - from the documentation.

edit: As you discovered, and stated in the comments, the easiest thing to do will be to just print the df.

if you want to stay in the notebook and control the number of columns that get printed (max of 15 before it automatically wraps)

# make up a wide empty datframe
col_names = ['a_' + str(x) for x in range(37)]
df = pd.DataFrame(columns=col_names, index=range(10))

section_width = 10 # set the number of columns you want to print - not that more than 15 

for section_no in range(int(df.shape[1] / section_width + 1)) :
    print(df.iloc[0:5, section_no * section_width : (section_no + 1) * section_width])
# if you want more or less than 5 rows then adust the numbers in .iloc[0:5]

Anyway, if you can give us some more detail on what you are trying to achieve we might be better able to help - Goodluck

Upvotes: 1

Max Kaha
Max Kaha

Reputation: 922

Have you tried to toggle the scrolling in the Cell options ? Like such:

enter image description here

Can be done for individual cells and all cells as you see in my screenshot.

Upvotes: 4

Related Questions