Home > database >  Select year from object type field using python and streamlit
Select year from object type field using python and streamlit

Time:03-02

I have a dataframe that includes a column "birthdate" but its of type object like this 1984-11-15 format y%-%M-%d. I want to convert it to date and just extract the year with no duplication in order to filter the dataframe based on the year.

I am using the multiselect option in order to allow the user to filter the dataframe choosing multiple options, but when i try to convert the column type the system crash and display the below error.

StreamlitAPIException : Every Multiselect default value must exist in options

Traceback:

File "f:\AIenv\streamlit\TEC_APPS\fit_in_out_leb.py", line 762, in <module>
    main()File "f:\AIenv\streamlit\TEC_APPS\fit_in_out_leb.py", line 366, in main
    db_stb =  st.sidebar.multiselect("Select  date of birth ",list(query_bd),default = query_bd)

what this error mean and how to fix it ?

code:


df['birthdate'] = pd.to_datetime(df['birthdate'])
query_bd = df.birthdate.unique()
db_stb =  st.sidebar.multiselect("Select  date of birth ",list(query_bd),default = query_bd)

CodePudding user response:

Create a new column for year, this column is used to generate the unique options and select a frame based on year.

import streamlit as st
import pandas as pd

d = {
    'birthdate': ['2022-01-01', '2021-01-01', '2020-01-01', '2019-01-01', '2019-02-05']
}
df = pd.DataFrame(d)
df['birthdate'] = pd.to_datetime(df['birthdate'])
df['year'] = df['birthdate'].dt.year

query_bd = df.year.unique()
db_stb =  st.sidebar.multiselect("Select  date of birth ", query_bd, default=query_bd)

selected = df.loc[df['year'].isin(db_stb)]
st.write(selected['birthdate'].dt.date)

Output

enter image description here

  • Related