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
1 Answer
Reset to default 0I 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 python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742324354a2453425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论