19

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

4 Answers 4

24

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

Sign up to request clarification or add additional context in comments.

8 Comments

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?
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)
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.
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
for some reason the javascript line gives error
|
18

I'd strongly recommend dtale as a more sophisticated way to display DataFrames in a notebook. It has scrolling, but it also efficiently handles large DataFrames, and allows editing and sorting, among other things.


If you don't want an additional library (for some reason):

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:

3 Comments

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("<div style='max-height: 400px'>" + pd.DataFrame(records, columns=columns).to_html() + "</div>"))
This works great. Is there a way to overwrite the standard display function used for rendering pd.DataFrame with this solution?
This is great, thank you. Any idea if it can be made faster for larger DataFrames?
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

This is what it looks like:

enter image description here

4 Comments

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?
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.
For me that just returns an empty cell. plotly-5.16.1 on Jupyter Notebook 7.0.3
Works on Jupyter Lab 4.0.5 (so doesn't work on Jupyter Notebook, only Jupyter Lab)
0

Using ipydatagrid:

import string
import numpy as np
import pandas as pd

N = 5000
cols = {char:np.random.randn(N) for char in string.ascii_lowercase[:5]} 
df = pd.DataFrame(cols)
from ipydatagrid import DataGrid
DataGrid(df)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.