i have a python project where i need to create a function that takes dataframe and list of conditions as arguments and return a dataframe based on the given conditions.
In my Streamlit project I have list of checkboxes where each checked box represent a condition like so :
these are the condition that the user can enable it in order to filter the datafarme.
Until now i am able to perform data filtering using if statement for each checkbox alone and with joining with each other but for this i have multiple if statement
.
I am sure that a function that takes dataframe
and list of checkboxes
as arguments can solve this.
code:
col1_ch,col2_ch,col3_ch = st.sidebar.columns(3)
with col1_ch:
adv_searchcheckbox_name_nickname = st.checkbox("Name or Nickname or Mother name",value = False,key=1)
adv_searchcheckbox_gender = st.checkbox("Gender",value = False,key=2)
adv_searchcheckbox_status_type = st.checkbox("Status type",value = False,key=3)
adv_searchcheckbox_country = st.checkbox("Country",value = False,key=4)
adv_searchcheckbox_bd = st.checkbox("Date Of Birth",value = False,key=5)
if adv_searchcheckbox_name_nickname:
col1, col2,col3 = st.sidebar.columns(3)
with col1:
name_search = st.text_input("name")
with col2:
nickname_search = st.text_input("nickname")
with col3:
Mother_name_search = st.text_input("mother name")
if adv_searchcheckbox_gender:
radio_gender = st.sidebar.radio(label="Gender", options=["M","F"])
if st.sidebar.button("search"):
# *******************name nickname mothername checkbox***************
# . only name/nickname/mother name is checked
if adv_searchcheckbox_name_nickname and not adv_searchcheckbox_gender and not adv_searchcheckbox_status_type and not adv_searchcheckbox_country and not adv_searchcheckbox_bd:
# if name is specified but not the nickname and mother name
if name_search != '' and nickname_search == '' and Mother_name_search =='':
df_result_search = df[df['name'].str.contains(name_search, case=False, na=False)]
# if nickname is specified but not the name and mother name
elif nickname_search != ''and name_search == '' and Mother_name_search == '':
df_result_search = df[df['nickname'].str.contains(nickname_search, case=False, na=False)]
# if mother name is specified but not the name and nickname
elif Mother_name_search != '' and name_search == '' and nickname_search == '':
df_result_search = df[df['mother_name'].str.contains(Mother_name_search, case=False, na=False)]
# if both name and nickname are specified
elif name_search != '' and nickname_search != '' and Mother_name_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 both name and mother_name are specified
elif name_search != '' and Mother_name_search!='' and nickname_search == '' :
df_result_search = df[(df['name'].str.contains(name_search, case=False, na=False)) & df['mother_name'].str.contains(Mother_name_search, case=False, na=False)]
# if both nickname and mother_name are specified
elif nickname_search != '' and Mother_name_search!='' and name_search == '':
df_result_search = df[(df['nickname'].str.contains(nickname_search, case=False, na=False)) & df['mother_name'].str.contains(Mother_name_search, case=False, na=False)]
# if user does not enter anything
else:
st.warning('Please enter at least a name or a nickname or mother name ')
# *******************name nickname mothername checkbox***************
# *******************gender checkbox***************
elif adv_searchcheckbox_gender and adv_searchcheckbox_name_nickname and not adv_searchcheckbox_status_type and not adv_searchcheckbox_country and not adv_searchcheckbox_bd:
# if name is specified but not the nickname and mother name
if name_search != '' and radio_gender !='' and nickname_search == '' and Mother_name_search =='':
df_result_search = df[df['name'].str.contains(name_search, case=False, na=False) & (df['gender'] ==(radio_gender))]
# if nickname is specified but not the name and mother name
elif nickname_search != '' and radio_gender !=''and name_search == '' and Mother_name_search == '':
df_result_search = df[df['nickname'].str.contains(nickname_search, case=False, na=False)& (df['gender'] ==(radio_gender))]
# if mother name is specified but not the name and nickname
elif Mother_name_search != '' and radio_gender !='' and name_search == '' and nickname_search == '' :
df_result_search = df[df['mother_name'].str.contains(Mother_name_search, case=False, na=False)& (df['gender'] ==(radio_gender))]
# if both name and nickname are specified
elif name_search != '' and nickname_search != '' and radio_gender !=''and Mother_name_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)) & (df['gender'] ==(radio_gender))]
# if both name and mother name are specified
elif name_search != '' and radio_gender !=''and Mother_name_search!='' and nickname_search == '':
df_result_search = df[(df['name'].str.contains(name_search, case=False, na=False)) & (df['mother_name'].str.contains(Mother_name_search, case=False, na=False)) & (df['gender'] ==(radio_gender))]
# if both nickname and mother name are specified
elif nickname_search != '' and radio_gender !=''and Mother_name_search!='' and name_search == '':
df_result_search = df[(df['nickname'].str.contains(nickname_search, case=False, na=False)) & (df['mother_name'].str.contains(name_search, case=False, na=False)) & (df['gender'] ==(radio_gender))]
# if all name nickname and mother name are specified
elif nickname_search != '' and radio_gender !=''and Mother_name_search!='' and name_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)) & (df['mother_name'].str.contains(name_search, case=False, na=False)) & (df['gender'] ==(radio_gender))]
# if user does not enter anything
else:
st.warning('Specify at least 1 input ')
# *******************gender checkbox***************
This is just for the first 2 checkboxes