17

I have a large (vertically) pandas dataframe that I would like to display as a nice table with scrollbars. I can get it to display the table with all the rows but I cannot get the scrollbars to display.

def data(x):
    strData = strData[['Data1','Data2','Data3']]
    display(strData)

output: No vertical scrollbars

enter image description here

magicsword
  • 1,179
  • 3
  • 16
  • 26

3 Answers3

21

Not sure if this is what you mean, but I guess you need to set the max_rows option to None, so that pandas doesn't put a limit on the number of rows displayed:

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

enter image description here


Update:

In [27]: 
##javascript
IPython.OutputArea.auto_scroll_threshold = 10;

In[28]:
def display_():    
    pd.set_option("display.max_rows", None)
    from IPython.core.display import display 
    display(df) #df must be defined up there

enter image description here

adir abargil
  • 5,495
  • 3
  • 19
  • 29
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • That's exactly what I need. I've added that to the code but it still shows me all the rows and no scrollbars. I tried print(strData), and just strData inside the function but it does not work. Does it behave differently when it is inside a function? – magicsword Mar 10 '17 at 17:43
  • Just tested inside a function. This works for me. `def display_(): pd.set_option("display.max_rows", None) from IPython.core.display import display display(df)` – Psidom Mar 10 '17 at 17:52
  • When I use print(strData) the data is not in a nice table anymore. It just appear as regular data (no table outline) the rows will be dynamic depending ton length of files. They can be from 30 rows to 300. This is why I wanted to add scrollbars to the data. – magicsword Mar 10 '17 at 17:53
  • 1
    30 rows are not enough to trigger the scroll bar of python notebook yet. You need to change the threshold for *auto_scrolling*. Try the updated method – Psidom Mar 10 '17 at 18:00
  • 1
    for some reason the javascript line gives error – adir abargil Apr 05 '21 at 11:03
  • `Javascript Error: IPython is not defined` – James Hirschorn Oct 02 '22 at 14:14
  • scrolling does not work for me with this solution – joAschauer Jan 19 '23 at 10:17
  • What is the context? Jupyter notebook, or jupyter-lab? – James Hirschorn Mar 20 '23 at 20:36
16

The other answer didn't work for me - IPython.OutputArea doesn't seem to exist any more (as far as I can tell, at least not in VSCode and based on the IPython code).

I managed to make a DataFrame scrollable with a somewhat hacky solution of generating the HTML table for the DataFrame and then putting that into a div, which can be made scrollable using CSS. In this case all we really need to do is set the height to some pixel value.

We can also set the overflow to auto and the width to fit-content so the scrollbar appears right next to the DataFrame, instead of all the way on the right of the window.

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>"))

Demo:

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • this works great, I love this solution! the only change I would make is replace `height` with `max-height` to prevent the table to take up unnecessary space when there are few items in it: ```display(HTML("
    " + pd.DataFrame(records, columns=columns).to_html() + "
    "))```
    – zaaath Sep 13 '22 at 16:59
  • This works great. Is there a way to overwrite the standard display function used for rendering pd.DataFrame with this solution? – Bananeen May 26 '23 at 21:16
7

Just pass the dataframe and observe the magic.

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
  • 2
    Could you explain why plotly is a good library to use in this case, and perhaps show a screenshot of what it would look like? – totokaka Feb 14 '22 at 11:00
  • 1
    The thing is I am quite comfortable in plotly. It gives more information closet than that of matplotlib and seaborn. Rest depends upon the user to go with another libraries too. – ASAD ASHRAF KAREL Feb 16 '22 at 09:32