Hi everybody,
i would like that my multiselect widget show data from dataframe in alphabetical order.
I haven’t found, in the widget parameters, a solution to this problem (but I could be wrong).
Is it a Pandas job? If yes, can someone give me some input on how to solve?
Down here a small example of my code:
companies = st.sidebar.multiselect('Filter the company:', options=df["Company"].unique())
df_sel_1 = df.query("Company == @companies")
Thanks
Hi @Andyton85
You’re right! You can call pandas’ .sort_values() function on your DataFrame like this example:
Solution
import streamlit as st
import pandas as pd
@st.experimental_memo
def load_data():
companies = [
"Google",
"Apple",
"Facebook",
"Facebook",
"Microsoft",
"Amazon",
"IBM",
]
return pd.DataFrame({"Company": companies})
df = load_data()
st.subheader("Unsorted Data")
st.dataframe(df)
st.subheader("Sorted unique values")
st.dataframe(df.sort_values(by="Company").Company.unique())
companies = st.multiselect(
"Filter the company:", options=df.sort_values(by="Company").Company.unique()
)
Output
Best,
Snehan
1 Like
Thanks a lot for your kind and quick reply
And also today i learn something new…
Bye
1 Like
system
Closed
February 1, 2023, 7:16am
4
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.