Ignore None / NAN values in st.dataframe sorting

Having a dataframe that contains NAN or None values, sorting treats those as smallest values.
In many situations this can be undesired.

I think it would be great to have them always at the bottom of the sorted column, regardless of sorting order, either by changing the default behavior or by having an option in st.dataframe.

Example:

import streamlit as st
import pandas as pd

df = pd.DataFrame({
    "A": [1, 3, None, 2, None],
    "B": [1.5, None, 5.2, 8.9, None]
})
st.dataframe(df)

PS:
This was brought up in a previous, now closed and non-answered, post here.

I think this would make a good feature request.

1 Like

There’s not a way to do it in streamlit, per se, but you can do

df_sorted = df.sort_values("A", na_position="last")

in pandas before passing it to streamlit (or when passing it).

Your example shows something interesting, though, which is that it’s not possible to put ALL the None’s at the bottom unless you’re willing to re-arrange individual columns independently. You can put the None’s from A at the bottom, or the None’s from B, but there’s not any way to do both (unless individual columns are really just independent lists, and you do it before passing them to pandas).


Thanks for the suggestion! This is a good workaround for the initial sorting, but of course does not allow for dynamic sorting in the UI.
Indeed, sorting can never put all None’s at the end when columns are not independent (if they were, I would probably not put them in the same dataframe anyways), so that’s fine. But for the sorted column, I would like the None’s to be at the bottom.

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