magicsword
magicsword

Reputation: 1289

Pandas DataFrame Table Vertical Scrollbars

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

Upvotes: 19

Views: 47657

Answers (4)

ASAD ASHRAF KAREL
ASAD ASHRAF KAREL

Reputation: 71

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

Upvotes: 7

robertspierre
robertspierre

Reputation: 4361

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

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55609

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:

Upvotes: 18

akuiper
akuiper

Reputation: 214957

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

Upvotes: 24

Related Questions