# Sorting in st.dataframe

**URL:** <https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483>\
**Category:** Using Streamlit\
**Tags:** pandas, dataframe, discussion\
**Created:** [March 25, 2024, 12:24pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483 "2024-03-25T12:24:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [March 25, 2024, 12:24pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/1 "2024-03-25T12:24:14Z")

</div>

![image](https://global.discourse-cdn.com/streamlit/original/3X/5/5/55d39884136b5ef4886085c11d54b38b1bf4e81e.png)  
So, I have a dataframe similar to the image attached. Is it possible to exclude the ‘Total’ row from sorting, I have look at the documentation of st.dataframe but there it is not specified how to exclude certain row while sorting

---

<div class="post-metadata">

**Author:** ![dataprofessor](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/dataprofessor/32/44163_2.png) [@dataprofessor](https://discuss.streamlit.io/u/dataprofessor)\
**Post date:** [March 25, 2024, 1:17pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/2 "2024-03-25T13:17:33Z")

</div>

Hi @Ameen

This can be done by using data slicing.

To exclude the last row of your DataFrame `df`, you can use:

```auto
df[:-1]

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [March 25, 2024, 1:41pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/3 "2024-03-25T13:41:11Z")

</div>

Hi @dataprofessor, thank you for your response. However, my intention isn’t to remove the row from being displayed rather, I’m looking to exclude it from the sorting process… while using st.dataframe , it offers built-in sorting for each column, but I want to prevent the row labeled ‘Total’ from being affected by this sorting. Is there a way to achieve that?

---

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [March 25, 2024, 1:55pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/4 "2024-03-25T13:55:41Z")

</div>

![image](https://global.discourse-cdn.com/streamlit/original/3X/d/b/dbde5eb3a4e592a557afea77b388bd403e66bcdb.png)

When I use the built-in sorting feature of `st.dataframe` , it correctly sorts the values. However, I would like to exclude the ‘Total’ row from sorting; I need it to stay at the bottom when sorting is applied. Is there a way to achieve this?

---

<div class="post-metadata">

**Author:** ![Guna\_Sekhar\_Venkata](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/guna_sekhar_venkata/32/44735_2.png) [@Guna\_Sekhar\_Venkata](https://discuss.streamlit.io/u/Guna_Sekhar_Venkata)\
**Post date:** [March 25, 2024, 3:06pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/5 "2024-03-25T15:06:08Z")

</div>

Hey @Ameen . Then first subset the data except the last row and sort the remaining dataframe at last add the last two the sorted dataframe.

Hope it works well !!!

Happy Streamlit-ing 🎈

---

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [March 25, 2024, 3:32pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/6 "2024-03-25T15:32:21Z")

</div>

> [@Guna\_Sekhar\_Venkata](#):
>
> first subset the data except the last row and sort the remaining dataframe at last add the last two the sorted dataframe.

Thanks for the reply, could you please provide a code example? I didn’t fully understand your explanation.

---

<div class="post-metadata">

**Author:** ![Guna\_Sekhar\_Venkata](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/guna_sekhar_venkata/32/44735_2.png) [@Guna\_Sekhar\_Venkata](https://discuss.streamlit.io/u/Guna_Sekhar_Venkata)\
**Post date:** [March 25, 2024, 4:33pm UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/7 "2024-03-25T16:33:26Z")

</div>

Hey @Ameen . Here is an demo:-

```auto
import pandas as pd
data = {
    'a': [54, 24, 78],
    'b': [104, 78, 182],
    'c': [324, 56, 380]
}
df = pd.DataFrame(data,index=['q1','q2','total'])
print('Before:\n',df)
df1=df.copy()
df=df1.iloc[:-1,:].sort_values(by=['a','b','c'],ignore_index=True)
df.loc[len(df.index)]=df1.loc['total']
df.rename(index={0:'q1',1:'q2',2:'total'},inplace=True)
print('-'*10)
print('After:\n',df)

Output:-
Before:
         a b c
q1 54 104 324
q2 24 78 56
total 78 182 380
----------
After:
         a b c
q1 24 78 56
q2 54 104 324
total 78 182 380

```

Hope you understood now !

Happy Streamlit-ing 🎈

---

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [March 26, 2024, 5:58am UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/8 "2024-03-26T05:58:42Z")

</div>

Hi @Guna_Sekhar_Venkata, thank you for your reply. I was hoping to find a solution using st.dataframe, but it seems that’s not possible. I will have to use the workaround you shared. Thanks again…

---

<div class="post-metadata">

**Author:** ![Ameen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ameen/32/28356_2.png) [@Ameen](https://discuss.streamlit.io/u/Ameen)\
**Post date:** [April 2, 2024, 8:54am UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/9 "2024-04-02T08:54:42Z")

</div>

if anyone is struggling with similar issue use streamlit-aggrid, it provide way more table configuration that st.dataframe

```auto
from st_aggrid import AgGrid, ColumnsAutoSizeMode, GridUpdateMode, JsCode
from st_aggrid.grid_options_builder import GridOptionsBuilder

# pass your dataframe inside gridoptionbuilder
gob = GridOptionsBuilder.from_dataframe(df)

pinned_row_data = [df[df["Quarter"] == "Total"].to_dict(orient="records")[0]]
df = df.drop(df[df["Quarter"] == "Total"].index)
height = 180
gob.configure_grid_options(pinnedBottomRowData=pinned_row_data)
gridOptions = gob.build()

AgGrid(df, height=height, gridOptions=gridOptions, fit_columns_on_grid_load=True,update_mode=GridUpdateMode.VALUE_CHANGED)

```

Using this sample code, you can pin a row to ensure it remains excluded during sorting and filtering operations.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [April 4, 2024, 8:55am UTC](https://discuss.streamlit.io/t/sorting-in-st-dataframe/65483/10 "2024-04-04T08:55:27Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
