admin管理员组

文章数量:1333729

I have a pandas dataframe with six date columns with YYYY-MM-DD formatting and a datatype of datetime64[ns]. I want to apply a styler to the dataframe to freeze the column names when I do a vertical scroll. However, when I apply the styler, it adds a timestamp to the columns with dates. Is there a way to prevent styler from adding the timestamp? Alternatively, if you know of a way to freeze column headers without using styler in Jupyter notebooks, I am open to those suggestions as well.

Code for styler:

def freeze_header(df_all):
    # Create a Styler object
    styler = df_all.style
    
    # Add CSS for sticky header
    styler.set_table_styles([
        {'selector': 'th', 'props': [('position', 'sticky'), 
                                     ('top', '0'), 
                                     ('background-color', 'white'), 
                                     ('z-index', '1'),
                                     ('border', '2px solid')]}
    ])
    return styler

freeze_header(df_all)

I have a pandas dataframe with six date columns with YYYY-MM-DD formatting and a datatype of datetime64[ns]. I want to apply a styler to the dataframe to freeze the column names when I do a vertical scroll. However, when I apply the styler, it adds a timestamp to the columns with dates. Is there a way to prevent styler from adding the timestamp? Alternatively, if you know of a way to freeze column headers without using styler in Jupyter notebooks, I am open to those suggestions as well.

Code for styler:

def freeze_header(df_all):
    # Create a Styler object
    styler = df_all.style
    
    # Add CSS for sticky header
    styler.set_table_styles([
        {'selector': 'th', 'props': [('position', 'sticky'), 
                                     ('top', '0'), 
                                     ('background-color', 'white'), 
                                     ('z-index', '1'),
                                     ('border', '2px solid')]}
    ])
    return styler

freeze_header(df_all)
Share Improve this question asked Nov 20, 2024 at 22:35 swilsonswilson 557 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I tried different approaches and the only one that worked was to transform the dataframe to HTML and style the output table using CSS. I am attaching the code I used to achieve that:

import pandas as pd
from IPython.display import HTML

data = {
    'Date1': pd.date_range(start='2023-01-01', periods=100, freq='D'),
    'Date2': pd.date_range(start='2023-02-01', periods=100, freq='D'),
    'Date3': pd.date_range(start='2023-03-01', periods=100, freq='D'),
    'Date4': pd.date_range(start='2023-04-01', periods=100, freq='D'),
    'Date5': pd.date_range(start='2023-05-01', periods=100, freq='D'),
    'Date6': pd.date_range(start='2023-06-01', periods=100, freq='D')
}

df_all = pd.DataFrame(data)

def render_dataframe_with_frozen_header(df):
    html = df.to_html(index=False)
    
    styles = """
    <style>
    .dataframe-container {
        max-height: 400px;
        overflow-y: scroll;
        position: relative;
    }
    .dataframe-container thead th {
        position: sticky;
        top: 0;
        background-color: white;
        z-index: 1;
        border: 2px solid;
    }
    </style>
    """
    
    html = f"{styles}<div class='dataframe-container'>{html}</div>"
    
    display(HTML(html))

render_dataframe_with_frozen_header(df_all)

本文标签: Prevent styler from adding timestamps to multiple date columns in pandas dataframe pythonStack Overflow