如何使用 streamlit 和 python 根据用户输入过滤数据框
How to filter dataframe based on user input using streamlit and python
我有一个包含 3 列的 datafarme 我希望允许用户根据用户输入过滤结果,用户可以 select 使用复选框过滤哪一列。
问题是,如果用户不选中第 2 个复选框,它 return 会出现以下错误:
df_result_search = df[df['age']==(age_search)]
NameError: name 'age_search' is not defined
如果用户选中所有复选框并输入值,它 return 一条错误记录,而它只接受一个值并根据它进行过滤。
代码:
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack','Tom'],
'nickname':['jack','krish','karim','joe','joe'],
'age':[20, 18, 19, 18,22]}
df = pd.DataFrame(data)
df_result_search = pd.DataFrame()
searchcheckbox_name_nickname = st.checkbox("Name or Nickname ",value = False,key=1)
searchcheckbox_age = st.checkbox("age",value = False,key=2)
if searchcheckbox_name_nickname:
name_search = st.text_input("name")
nickname_search = st.text_input("nickname")
if searchcheckbox_age:
age_search = st.number_input("age",min_value=0)
if st.button("search"):
df_result_search = df[df['name'].str.contains(name_search,case=False, na=False)]
df_result_search = df[df['nickname'].str.contains(nickname_search,case=False, na=False)]
df_result_search = df[df['age']==(age_search)]
st.write("{} Records ".format(str(df_result_search.shape[0])))
st.dataframe(df_result_search)
代码已更新,如果情况 3 pass # continue here.
,您可以在选中所有复选框后继续编写代码。
代码
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack', 'Tom'],
'nickname':['jack','krish','karim','joe', 'joe'],
'age':[20, 18, 19, 18, 22]}
df = pd.DataFrame(data)
st.write(df)
df_result_search = pd.DataFrame()
searchcheckbox_name_nickname = st.checkbox("Name or Nickname ",value = False,key=1)
searchcheckbox_age = st.checkbox("age",value = False,key=2)
if searchcheckbox_name_nickname:
name_search = st.text_input("name")
nickname_search = st.text_input("nickname")
else:
name_search = ''
nickname_search = ''
if searchcheckbox_age:
age_search = st.number_input("age",min_value=0)
else:
age_search = 0
if st.button("search"):
# 1. only name/nickname is checked
if searchcheckbox_name_nickname and not searchcheckbox_age:
# if name is specified but not the nickname
if name_search != '' and nickname_search == '':
df_result_search = df[df['name'].str.contains(name_search, case=False, na=False)]
# if nickname is specified but not the name
elif name_search == '' and nickname_search != '':
df_result_search = df[df['nickname'].str.contains(nickname_search, case=False, na=False)]
# if both name and nickname are specified
elif name_search != '' and nickname_search != '':
df_result_search = df[(df['name'].str.contains(name_search, case=False, na=False)) & (df['nickname'].str.contains(nickname_search, case=False, na=False))]
# if user does not enter anything
else:
st.warning('Please enter at least a name or a nickname')
# 2. only age is checked
elif not searchcheckbox_name_nickname and searchcheckbox_age:
if age_search != 0:
df_result_search = df[df['age'] == age_search]
# 3. if both name/nickname and age are checked
else:
pass # continue here.
st.write("{} Records ".format(str(df_result_search.shape[0])))
st.dataframe(df_result_search)
示例输出
用户只使用姓名复选框并且只填写姓名。
我有一个包含 3 列的 datafarme 我希望允许用户根据用户输入过滤结果,用户可以 select 使用复选框过滤哪一列。
问题是,如果用户不选中第 2 个复选框,它 return 会出现以下错误:
df_result_search = df[df['age']==(age_search)]
NameError: name 'age_search' is not defined
如果用户选中所有复选框并输入值,它 return 一条错误记录,而它只接受一个值并根据它进行过滤。
代码:
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack','Tom'],
'nickname':['jack','krish','karim','joe','joe'],
'age':[20, 18, 19, 18,22]}
df = pd.DataFrame(data)
df_result_search = pd.DataFrame()
searchcheckbox_name_nickname = st.checkbox("Name or Nickname ",value = False,key=1)
searchcheckbox_age = st.checkbox("age",value = False,key=2)
if searchcheckbox_name_nickname:
name_search = st.text_input("name")
nickname_search = st.text_input("nickname")
if searchcheckbox_age:
age_search = st.number_input("age",min_value=0)
if st.button("search"):
df_result_search = df[df['name'].str.contains(name_search,case=False, na=False)]
df_result_search = df[df['nickname'].str.contains(nickname_search,case=False, na=False)]
df_result_search = df[df['age']==(age_search)]
st.write("{} Records ".format(str(df_result_search.shape[0])))
st.dataframe(df_result_search)
代码已更新,如果情况 3 pass # continue here.
,您可以在选中所有复选框后继续编写代码。
代码
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack', 'Tom'],
'nickname':['jack','krish','karim','joe', 'joe'],
'age':[20, 18, 19, 18, 22]}
df = pd.DataFrame(data)
st.write(df)
df_result_search = pd.DataFrame()
searchcheckbox_name_nickname = st.checkbox("Name or Nickname ",value = False,key=1)
searchcheckbox_age = st.checkbox("age",value = False,key=2)
if searchcheckbox_name_nickname:
name_search = st.text_input("name")
nickname_search = st.text_input("nickname")
else:
name_search = ''
nickname_search = ''
if searchcheckbox_age:
age_search = st.number_input("age",min_value=0)
else:
age_search = 0
if st.button("search"):
# 1. only name/nickname is checked
if searchcheckbox_name_nickname and not searchcheckbox_age:
# if name is specified but not the nickname
if name_search != '' and nickname_search == '':
df_result_search = df[df['name'].str.contains(name_search, case=False, na=False)]
# if nickname is specified but not the name
elif name_search == '' and nickname_search != '':
df_result_search = df[df['nickname'].str.contains(nickname_search, case=False, na=False)]
# if both name and nickname are specified
elif name_search != '' and nickname_search != '':
df_result_search = df[(df['name'].str.contains(name_search, case=False, na=False)) & (df['nickname'].str.contains(nickname_search, case=False, na=False))]
# if user does not enter anything
else:
st.warning('Please enter at least a name or a nickname')
# 2. only age is checked
elif not searchcheckbox_name_nickname and searchcheckbox_age:
if age_search != 0:
df_result_search = df[df['age'] == age_search]
# 3. if both name/nickname and age are checked
else:
pass # continue here.
st.write("{} Records ".format(str(df_result_search.shape[0])))
st.dataframe(df_result_search)
示例输出
用户只使用姓名复选框并且只填写姓名。