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.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.