Skip to content

Sticky header and index for large data frames in Jupyter  #29072

Closed
@ghost

Description

Problem description

When displaying a large data frame in Jupyter the number of columns will be limited by max_cols set in the default setting, and all the rows will be displayed.

I would like to add an option in the default settings so that large data frames will be displayed with a sticky header and index and then be able to scroll though the data frame.

Proof of concept solution

Following the solution for html tables found at Stackoverflow: Table with fixed header and fixed column on pure css with the solution shown in action here HTML and CSS Solution
I came up with the following solution (which follows the same way of <style scoped> as the _repr_html_ method):

import numpy as np
import pandas as pd
from IPython.display import HTML

# Dummy dataframe
columns = [chr(i) for i in range(ord('a'),ord('z')+1)]
data = np.random.rand(len(columns),len(columns))
df = pd.DataFrame(data, columns=columns)

# Solution
# Getting default html as string
df_html = df.to_html() 
# CSS styling 
style = """
<style scoped>
    .dataframe-div {
      max-height: 300px;
      overflow: auto;
      position: relative;
    }

    .dataframe thead th {
      position: -webkit-sticky; /* for Safari */
      position: sticky;
      top: 0;
      background: black;
      color: white;
    }

    .dataframe thead th:first-child {
      left: 0;
      z-index: 1;
    }

    .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

    .dataframe tbody tr th {
      position: -webkit-sticky; /* for Safari */
      position: sticky;
      left: 0;
      background: black;
      color: white;
      vertical-align: top;
    }
</style>
"""
# Concatenating to single string
df_html = style+'<div class="dataframe-div">'+df_html+"\n</div>"

# Displaying df with sticky header and index
HTML(df_html)


I would therefore like to know if others also would like to have this feature in pandas?
Otherwise I guess I would make it to an independent module that wraps the _repr_html_ method.
I know that it is not just a matter of adding the new styling above for the general case, but the above solution is a minimal working solution.
A related issues is #28091

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsDocsOutput-Formatting__repr__ of pandas objects, to_stringStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions