I have a datafarme that includes 3 columns i want to allow the user to filter the result based on the user input where the user can select which column to filter using a checkbox.
The problem is that if the user doesn’t check the 2 checkbox it return the below error :
df_result_search = df[df[‘age’]==(age_search)]
NameError: name ‘age_search’ is not defined
and if the user check all the checkbox and enter the values it return a wrong record while it takes only one value and filter based on it.
code:
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack'],
'nickname':['jack','krish','karim','joe'],
'age':[20, 18, 19, 18]}
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)
The app also runs into errors when the user directly clicks on search before checking either box. Here’s one way to ensure that both name and age are entered:
import streamlit as st
import pandas as pd
data = {'name':['Tom', 'nick', 'krish', 'jack'],
'nickname':['jack','krish','karim','joe'],
'age':[20, 18, 19, 18]}
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"):
if not searchcheckbox_name_nickname or not searchcheckbox_age:
st.error('Please enter both name **and** age.')
else:
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)
yes this was another error but what about my question how to solve it ?
first the user must check the check box so if he check the name or nickname he must enter a valid name or nickname that exist in the data … if he check the age he must enter a valid age that exist in the data… so the filter is done based on the checked column/s…
otherwise it return empty or not exist data.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.