St.dataframe columns freeze

Is it possible to freeze first 2 or 3 columns in st.dataframe like in excel.
Sai

Hi @Sai_SaiGraph,

As of now, Streamlit doesn’t have a native way to freeze table columns like Excel.

However, you can achieve this with Ag-grid. You can use Ag-grid in Streamlit via the Streamlit-aggrid component.

To freeze (or pin, in ag-grid terms) columns using streamlit-ag-grid, you can specify the pinned property in the columnDefs of gridOptions

Here’s an example:

import streamlit as st
from st_aggrid import AgGrid
import pandas as pd

df = pd.DataFrame({
    'A': range(10),
    'B': range(10, 20),
    'C': range(20, 30),
    'D': range(30, 40),
    'E': range(40, 50)
})

gridOptions = {
    'defaultColDef': {
        'resizable': True
    },
    'columnDefs': [
        {'headerName': 'A', 'field': 'A', 'pinned': 'left'},
        {'headerName': 'B', 'field': 'B', 'pinned': 'left'},
        {'headerName': 'C', 'field': 'C'},
        {'headerName': 'D', 'field': 'D'},
        {'headerName': 'E', 'field': 'E'}
    ]
}

AgGrid(df, gridOptions=gridOptions)

I hope this helps.

Best,
Charly

1 Like

Thanks Charly.