Hi everyone!
I have five checkboxes. When some of them are selected, I want my dataframe to be filtered on some specific condition. Additionally, values should be filtered with the AND logic. For example:
if filter1 & filter2:
df = df .loc[(some condition of filter1) & (some condition of filter2)]
However, having five filters, I have to use a lot of different combinations to implement such logic (e.g. if filter1 & filter3, if filter1 & filter4, if filter1 & filter3 & filter4, and so on).
Is there is a more effective way to do it without going through all the filters combinations in if statements?
I checked this solution, however, it is applicable for sliders only as I understand it:
Using your checkboxes, you can incrementally construct your combined filter, appending the result to querystr. (Eg. querystr = querystr + "Gender == ‘M’ "
Gender would be the dataframe field and ‘M’ could be check box value.
Similarly continue with your other checkboxes…
querystr = querystr + "Race == ‘White’ ") will make the resultant query string for Gender as well as Race… And so on…
Hi @Shawn_Pereira,
Thank you!
A brilliant solution and clear explanation. However, it’s not possible to simply concatenate strings as pandas.DataFrame.query requires an ‘and’ operator for multiple conditions. Thus, I did the check for each filter as in the next example.
if not querystr == "":
querystr = querystr + "**and** condition"
else:
querystr = querystr + "condition"
Since your fieldvalue is characters, you will not need to use the str function to join it with querystr.
After processing all your check boxes, check if
a. querystr is still empty. i.e
no condition is chosen at all, and
b. If querystr ends with + " and ". If yes, remove those many characters from the end of the querystr.
If you want to check and manage errors, you can consider using the try-except statements to process the final querystr.
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.