Using & operator, don't forget to wrap the sub-statements with ():

males = df[(df[Gender]=='Male') & (df[Year]==2014)]

To store your DataFrames in a dict using a for loop:

from collections import defaultdict
dic={}
for g in ['male', 'female']:
    dic[g]=defaultdict(dict)
    for y in [2013, 2014]:
        dic[g][y]=df[(df[Gender]==g) & (df[Year]==y)] #store the DataFrames to a dict of dict

A demo for your getDF:

def getDF(dic, gender, year):
    return dic[gender][year]

print genDF(dic, 'male', 2014)
Answer from zhangxaochen on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-pandas-dataframe-with-multiple-conditions
Filter Pandas Dataframe with multiple conditions - GeeksforGeeks
July 23, 2025 - In the above example, print(filtered_values) will give the output as (array([0], dtype=int64),) which indicates the first row with index value 0 will be the output. After that output will have 1 row with all the columns and it is retrieved as per the given conditions. In this approach, we get all rows having Salary lesser or equal to 100000 and Age < 40, and their JOB starts with ‘C’ from the dataframe. Its just query the columns of a DataFrame with a single or more Boolean expressions and if multiple, it is having & condition in the middle. ... # import module import pandas as pd # assign
Discussions

python - Filter Multiple Values using pandas - Stack Overflow
I am using Python and Pandas. I have a df that works similar to this: +--------+--------+-------+ | Col1 | Col2 | Col3 | +--------+--------+-------+ | Team 1 | High | Pizza | | Team 1 | ... More on stackoverflow.com
🌐 stackoverflow.com
Pandas - Filter based on multiple conditions
You can filter rows by using "boolean indexing", and as with all boolean expressions you can combine multiple conditions with "and", "or, "any", and "all" operations. Here is an example using "and", the syntax for that is " & "; note that neither condition on it's own would give the results that the conjunction of the 2 conditions does. >>> df = pd.DataFrame({"a":[1, 3, 5], "b":[2, 4, 6], "c":[7, 14, 1]}) >>> df a b c 0 1 2 7 1 3 4 14 2 5 6 1 >>> df[(df.c <= 7)] a b c 0 1 2 7 2 5 6 1 >>> df[(df.a <= 3)] a b c 0 1 2 7 1 3 4 14 >>> df[(df.a <= 3) & (df.c <= 7)] a b c 0 1 2 7 BTW, I don't see how having the column names as a list of strings would be much help, but you _could_ do something using the df["column"] syntax like: >>> def myfilt(df, labels): ... return df[(df[labels[0]] <= 3) & (df[labels[1]] <= 7)] ... >>> control_fields = ["a", "c"] >>> myfilt(df, control_fields) a b c 0 1 2 7 >>> https://pandas-docs.github.io/pandas-docs-travis/user_guide/indexing.html#boolean-indexing More on reddit.com
🌐 r/learnpython
6
1
January 17, 2021
How to "pass through" multiple conditions in a pandas dataframe with query?
Well you can simply use in e.g. df.query('temperature in temperatures') However - if there are any column names in your dataframe with the same name as your list - the column will take preference - which would cause undesired results. To avoid this possibility - you can use the regular boolean indexing df[ df.temperature.isin(temperatures) ] http://pandas.pydata.org/pandas-docs/stable/indexing.html#the-query-method-experimental More on reddit.com
🌐 r/learnpython
2
5
November 3, 2016
Panda - filter multiple columns by multiple values
You can use .isin(): RawData["Column3"].isin(["AAA", "BBB"]) More on reddit.com
🌐 r/learnpython
7
1
June 21, 2024
🌐
Reddit
reddit.com › r/learnpython › panda - filter multiple columns by multiple values
r/learnpython on Reddit: Panda - filter multiple columns by multiple values
June 21, 2024 -

I'm new to Python. I learned C++ in college years ago and recently have been using VBA in excel. I've learned you can use Python to manipulate excel files and am trying to learn a new skill and streamline a weekly reporting requirement.

I'm trying to filter the data across multiple columns and multiple values in some columns.

So far I have:

OutputData = RawData [ (RawData['Column1']=='ABC') & (RawData['Column2']!='XYZ') ]

This works thus far, but how to I get:

Column3 == AAA or BBB

Also, how would I exclude values starting with CCC

🌐
Medium
medium.com › @AnahattaSuputra › filtering-multiple-columns-based-on-values-in-pandas-dataframe-7c2cad450b50
FILTERING MULTIPLE COLUMNS BASED ON VALUES IN PANDAS DATAFRAME | by Anahatta Suputra | Medium
October 1, 2021 - Ok, we have extra steps. First we have to code how to collect all columns name. How to do that? Simple, you can type : And then we copy the previous code and mix them up and put them in a variable. ... As you can see, we can manage filtering data with that way above.
🌐
IncludeHelp
includehelp.com › python › how-do-you-filter-pandas-dataframes-by-multiple-columns.aspx
How do you filter pandas DataFrames by multiple columns?
To filter pandas DataFrame by multiple columns, we simply compare that column values against a specific condition but when it comes to filtering of DataFrame by multiple columns, we need to use the AND (&&) Operator to match multiple columns with multiple conditions.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › apply multiple filters to pandas dataframe or series
Apply Multiple Filters to Pandas DataFrame or Series - Spark By {Examples}
June 17, 2025 - By using df[], loc[], query() and isin() we can apply multiple filters for retrieving data efficiently from the pandas DataFrame or Series. Applying
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › pandas - filter based on multiple conditions
r/learnpython on Reddit: Pandas - Filter based on multiple conditions
January 17, 2021 -

Hi,

I have a csv file with approx. 100 columns and I want to filter rows if two of the columns are set to a value of X and the other columns are blank / Nan values. In order to make the code more readable, I would like to specify the column names in a list and then use the variable name within the Pandas query e.g. something like the following:

 

my_file=/home/test.csv
my_df=pd.read_csv(my_file)

control_fields=['Is_Active','Is_Valid']  
data_fields=['Age','DOB','Country','City']  

 

As a starting point, I have tried the following but this isn't filtering the data at all:

 

my_new_df=my_df[my_df['control_fields']==1]

 

Can someone please explain why the above isn't working and also advise if there is a better way of achieving my requirement?

Thanks!

🌐
w3resource
w3resource.com › python-exercises › pandas › filter › pandas-filter-exercise-17.php
Pandas: Filter by matching multiple values in a given dataframe - w3resource
September 6, 2025 - import pandas as pd # World alcohol consumption data new_w_a_con = pd.read_csv('world_alcohol.csv') print("World alcohol consumption sample data:") print(new_w_a_con.head()) print("\nFilter by matching multiple values in a given dataframe:") flt_wine = new_w_a_con["WHO region"].isin(["Africa", "Eastern Mediterranean", "Europe"]) print(new_w_a_con[flt_wine]) ... World alcohol consumption sample data: Year WHO region ... Beverage Types Display Value 0 1986 Western Pacific ... Wine 0.00 1 1986 Americas ... Other 0.50 2 1985 Africa ... Wine 1.62 3 1986 Americas ... Beer 4.27 4 1987 Americas ... Beer 1.98 [5 rows x 5 columns] Filter by matching multiple values in a given dataframe: Year WHO region ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › ways to filter pandas dataframe by column values
Ways to Filter Pandas DataFrame by Column Values
May 1, 2025 - To filter a Pandas DataFrame by multiple columns, you can use boolean indexing with logical operators (e.g., & for AND), the loc method to select specific rows and columns, or the query method for a more readable string-based approach.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › ways-to-filter-pandas-dataframe-by-column-values
Filter Pandas Dataframe by Column Value - GeeksforGeeks
July 15, 2025 - This code filters the DataFrame to include only rows where the "Age" column has values of either 25 or 45. The .query() method allows you to filter a DataFrame using SQL-like syntax. This can be particularly useful when dealing with complex conditions. ... import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32,45], 'Score': [85, 90, 78]} df = pd.DataFrame(data) # Filter using query method where Age > 30 and Score < 90 filtered_df = df.query('Age > 30 and Score < 90') print(filtered_df)
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas filter dataframe by multiple conditions
Pandas Filter DataFrame by Multiple Conditions - Spark By {Examples}
October 3, 2024 - How to Filter Pandas DataFrame by multiple conditions? By using df[], loc[], query(), eval() and numpy.where() we can filter Pandas DataFrame by multiple
🌐
pythontutorials
pythontutorials.net › blog › dataframe-filtering-rows-by-column-values
How to Filter Pandas DataFrame Rows by Multiple Column Values: Fix 'ValueError' and Shorten Your Code — pythontutorials.net
Filtering by multiple columns is a critical skill in pandas, but it doesn’t have to be error-prone or verbose. By mastering logical operators, isin(), and query(), you can efficiently subset data. Remember to fix ValueError by using parentheses, ...
🌐
Kaggle
kaggle.com › getting-started › 32512
The quickest way to filter a panda dataframe using multiple ...
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Statology
statology.org › home › how to filter a pandas dataframe on multiple conditions
How to Filter a Pandas DataFrame on Multiple Conditions
August 19, 2020 - by Zach Bobbitt Published on Published on August 19, 2020 · Often you may want to filter a pandas DataFrame on more than one condition. Fortunately this is easy to do using boolean operations. This tutorial provides several examples of how to filter the following pandas DataFrame on multiple conditions:
🌐
Saturn Cloud
saturncloud.io › blog › how-to-use-pandas-to-check-multiple-columns-for-a-condition
How to Use Pandas to Check Multiple Columns for a Condition | Saturn Cloud Blog
May 1, 2026 - Using boolean indexing to filter rows based on multiple conditions · Using the apply method to apply a function to multiple columns · The loc method is a powerful tool for selecting rows and columns from a Pandas dataframe based on specific ...
🌐
Built In
builtin.com › data-science › pandas-filter
How to Filter Pandas DataFrames | Built In
We’ve now selected the rows in which the value in the “val” column is greater than 0.5. The logical operators function also works on strings. f[df.name > 'Jane'] name ctg val val2 ------------------------------------------- 1 John A 0.67 1 3 Mike B 0.91 5 · Only the names that come after “Jane” in alphabetical order are selected. Pandas allows for combining multiple logical operators.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.6 documentation
The filtered subset of the DataFrame or Series. ... Access a group of rows and columns by label(s) or a boolean array.
🌐
Medium
medium.com › swlh › 3-ways-to-filter-pandas-dataframe-by-column-values-dfb6609b31de
3 ways to filter Pandas DataFrame by column values | by Padhma Muniraj | The Startup | Medium
February 15, 2022 - Inside .loc , the condition within the parentheses evaluates to a boolean value which is then applied upon the column specified. The data returned from multiple filters depends on the operation performed. When & and | operations are performed without an assignment, a series is returned.
🌐
Medium
medium.com › @debopamdeycse19 › how-to-filter-values-in-pandas-basic-to-advanced-methods-25b753ad74e5
How to Filter Values in Pandas- Basic to Advanced Methods | by Let's Decode | Medium
December 9, 2023 - Applying filters on multiple columns simultaneously: Pandas allow us to apply filters simultaneously by providing condition expressions for each column.