使用 python 和 Streamlit 从对象类型字段中选择年份
我有一个数据框,其中包含“生日”列,但其类型为 object,如 1984-11-15
格式 y%-%M-%d。 我想将其转换为日期,并仅提取无重复的年份,以便根据年份过滤数据帧。
我正在使用多选选项,以便允许用户选择多个选项来过滤数据帧,但是当我尝试转换列类型时,系统崩溃并显示以下错误。
StreamlitAPIException:每个多选默认值都必须存在于选项中
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)
此错误意味着什么以及如何修复它?
代码:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为年份创建一个新列,该列用于生成唯一选项并根据年份选择框架。
输出
Create a new column for year, this column is used to generate the unique options and select a frame based on year.
Output