使用 python 和 Streamlit 从对象类型字段中选择年份

发布于 2025-01-10 02:46:56 字数 811 浏览 0 评论 0原文

我有一个数据框,其中包含“生日”列,但其类型为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-17 02:46:56

为年份创建一个新列,该列用于生成唯一选项并根据年份选择框架。

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)

输出

enter图像描述在这里

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文