Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 685
magicsword Avatar asked Mar 10 '17 17:03

magicsword


3 Answers

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:

like image 98
Bernhard Barker Avatar answered Oct 22 '22 12:10

Bernhard Barker


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

like image 22
Psidom Avatar answered Oct 22 '22 12:10

Psidom


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
like image 2
ASAD ASHRAF KAREL Avatar answered Oct 22 '22 12:10

ASAD ASHRAF KAREL