Very likely, you're using the wrong type for the year. I imagine these are integers.

You should try:

df.loc[(df['Granularity'].isin(['Total', 'Urban'])) & df['Year'].eq(2017)]

output (for the Year 2018 as 2017 is missing from the provided data):

           Zone Granularity  Year      Value
20909  Zimbabwe       Total  2018  14438.802
20913  Zimbabwe       Urban  2018   5447.513
Answer from mozway on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Select rows by multiple conditions | note.nkmk.me
August 8, 2023 - pandas: Replace values based on conditions with where(), mask()
Discussions

python - Selecting with complex criteria from pandas.DataFrame - Stack Overflow
You can use pandas it has some built in functions for comparison. So if you want to select values of "A" that are met by the conditions of "B" and "C" (assuming you want back a DataFrame pandas object) 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
How to remove rows with multiple conditions?
If you want to subset where 1984 doesn't lose (wins, equal, or don't know) regardless of whether it is year 1 or year 2, your subset is (y1==1984&win!='Year 2')|(y2==1984&win!='Year2') and should be (y1==1984&win!='Year 2')|(y2==1984&win!='Year 1'). I changed the number and added a space. More on reddit.com
🌐 r/Rlanguage
4
1
January 23, 2021
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas loc[] multiple conditions
Pandas loc[] Multiple Conditions - Spark By {Examples}
June 24, 2025 - To select rows based on multiple conditions, use the Pandas loc[] attribute. The loc[] function in pandas allows you to select data based on labels or a
Top answer
1 of 5
543

Sure! Setup:

>>> import pandas as pd
>>> from random import randint
>>> df = pd.DataFrame({'A': [randint(1, 9) for x in range(10)],
                   'B': [randint(1, 9)*10 for x in range(10)],
                   'C': [randint(1, 9)*100 for x in range(10)]})
>>> df
   A   B    C
0  9  40  300
1  9  70  700
2  5  70  900
3  8  80  900
4  7  50  200
5  9  30  900
6  2  80  700
7  2  80  400
8  5  80  300
9  7  70  800

We can apply column operations and get boolean Series objects:

>>> df["B"] > 50
0    False
1     True
2     True
3     True
4    False
5    False
6     True
7     True
8     True
9     True
Name: B
>>> (df["B"] > 50) & (df["C"] != 900)

or

>>> (df["B"] > 50) & ~(df["C"] == 900)
0    False
1    False
2     True
3     True
4    False
5    False
6    False
7    False
8    False
9    False

[Update, to switch to new-style .loc]:

And then we can use these to index into the object. For read access, you can chain indices:

>>> df["A"][(df["B"] > 50) & (df["C"] != 900)]
2    5
3    8
Name: A, dtype: int64

but you can get yourself into trouble because of the difference between a view and a copy doing this for write access. You can use .loc instead:

>>> df.loc[(df["B"] > 50) & (df["C"] != 900), "A"]
2    5
3    8
Name: A, dtype: int64
>>> df.loc[(df["B"] > 50) & (df["C"] != 900), "A"].values
array([5, 8], dtype=int64)
>>> df.loc[(df["B"] > 50) & (df["C"] != 900), "A"] *= 1000
>>> df
      A   B    C
0     9  40  300
1     9  70  700
2  5000  70  900
3  8000  80  900
4     7  50  200
5     9  30  900
6     2  80  700
7     2  80  400
8     5  80  300
9     7  70  800
2 of 5
84

Another solution is to use the query method:

import pandas as pd

from random import randint
df = pd.DataFrame({'A': [randint(1, 9) for x in xrange(10)],
                   'B': [randint(1, 9) * 10 for x in xrange(10)],
                   'C': [randint(1, 9) * 100 for x in xrange(10)]})
print df

   A   B    C
0  7  20  300
1  7  80  700
2  4  90  100
3  4  30  900
4  7  80  200
5  7  60  800
6  3  80  900
7  9  40  100
8  6  40  100
9  3  10  600

print df.query('B > 50 and C != 900')

   A   B    C
1  7  80  700
2  4  90  100
4  7  80  200
5  7  60  800

Now if you want to change the returned values in column A you can save their index:

my_query_index = df.query('B > 50 & C != 900').index

....and use .iloc to change them i.e:

df.iloc[my_query_index, 0] = 5000

print df

      A   B    C
0     7  20  300
1  5000  80  700
2  5000  90  100
3     4  30  900
4  5000  80  200
5  5000  60  800
6     3  80  900
7     9  40  100
8     6  40  100
9     3  10  600
🌐
Statology
statology.org › home › how to select rows by multiple conditions using pandas loc
How to Select Rows by Multiple Conditions Using Pandas loc
October 25, 2021 - This tutorial explains how to select rows from a pandas DataFrame based on multiple conditions using the loc() function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-pandas-dataframe-with-multiple-conditions
Filter Pandas Dataframe with multiple conditions - GeeksforGeeks
July 23, 2025 - We are mentioning a list of columns that need to be retrieved along with the Boolean conditions and since many conditions, it is having '&'. Here, we get all rows having Salary lesser or equal to 100000 and Age < 40 and their JOB starts with ‘A’ from the dataframe. ... # import module import pandas as pd # assign data dataFrame = pd.DataFrame({'Name': [' RACHEL ', ' MONICA ', ' PHOEBE ', ' ROSS ', 'CHANDLER', ' JOEY '], 'Age': [30, 35, 37, 33, 34, 30], 'Salary': [100000, 93000, 88000, 120000, 94000, 95000], 'JOB': ['DESIGNER', 'CHEF', 'MASUS', 'PALENTOLOGY', 'IT', 'ARTIST']}) # filter dataframe display(dataFrame[dataFrame.eval("Salary <=100000 & (Age <40) & JOB.str.startswith('A').values")])
🌐
thisPointer
thispointer.com › home › pandas › pandas – select rows by conditions on multiple columns
Pandas - Select Rows by conditions on multiple columns - thisPointer
February 12, 2023 - Select rows in above DataFrame for which ‘Product’ column contains the value ‘Apples’, ... It will return a DataFrame in which Column ‘Product‘ contains ‘Apples‘ only i.e. Name Product Sale 0 jack Apples 34 3 Sonia Apples 32 5 Mike Apples 35 ... Will return a Series object of True & False i.e. 0 True 1 False 2 False 3 True 4 False 5 True Name: Product, dtype: bool · Series will contain True when condition is passed and False in other cases.
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › pandas_numpy › pandas_numpy-exercise-3.php
Filter DataFrame rows with multiple conditions in Pandas
Selecting Rows Based on Multiple Conditions: selected_rows = df[(df['Age'] > 25) & (df['Salary'] > 50000)] Uses boolean indexing to select rows where both conditions are true: age is greater than 25 and salary is greater than 50000.
🌐
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!

🌐
Reddit
reddit.com › r/learnpython › how to "pass through" multiple conditions in a pandas dataframe with query?
r/learnpython on Reddit: How to "pass through" multiple conditions in a pandas dataframe with query?
November 3, 2016 -

Users can use the where or query function with pandas dataframes to select rows/columns of the dataframe that match certain conditions, e.g.

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.query.html

>>> from numpy.random import randn
>>> from pandas import DataFrame
>>> df = DataFrame(randn(10, 2), columns=list('ab'))
>>> df.query('a > b')

In my case, it may be better to think of a simple conditional, all rows satisfying a==1

df.query('a==1')

Let's say I had a numpy array/Python list of values, and I would like to do an "OR" query for each item in the list.

list1 = [10, 20, 50]
# the query
df.query('a==10 | a==20 | a==50')

Is this possible? The idea would be I would write a function whereby users input a list of values to query, and it performs an OR query for each.

For where, the idea is similar:

temperatures = [80, 90, 100]
# reads in temperatures
# performs this query: 
rows = df.where('(temperature == 80) | (temperature == 90) | (temperature == 100)')
🌐
Pandas
pandas.pydata.org › docs › getting_started › intro_tutorials › 03_subset_data.html
How do I select a subset of a DataFrame? — pandas 3.0.6 documentation
Similar to the conditional expression, the isin() conditional function returns a True for each row the values are in the provided list. To filter the rows based on such a function, use the conditional function inside the selection brackets []. In this case, the condition inside the selection brackets titanic["Pclass"].isin([2, 3]) checks for which rows the Pclass column is either 2 or 3.
🌐
Medium
medium.com › @whyamit101 › pandas-select-rows-by-condition-f024e58d936e
pandas select rows by condition. The biggest lie in data science? That… | by why amit | Medium
April 12, 2025 - You can do so by combining conditions using the & operator. ny_over_25 = df[(df['City'] == 'New York') & (df['Age'] > 25)] print(ny_over_25) In this case, you need to wrap each condition in parentheses to ensure pandas processes them correctly.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-use-pandas-loc-with-multiple-conditions
How to Use Pandas loc with Multiple Conditions | Saturn Cloud Blog
May 1, 2026 - You can use loc to select data based on the following types of labels or conditions: A single label or list of labels for rows or columns ... One of the most powerful features of Pandas loc is the ability to select data based on multiple conditions.
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily filter pandas dataframes with multiple conditions using loc
How To Easily Filter Pandas DataFrames With Multiple Conditions Using Loc
December 3, 2025 - The structure for using multiple conditions within loc generally looks like this, where condition_X is a boolean expression applied to the DataFrame: df.loc[(condition_1) & (condition_2) | (condition_3)]. Remember that the outermost set of square brackets [] is the indexer for loc, and the conditions themselves must be grouped using parentheses () to ensure correct mathematical and logical operator precedence. We will now demonstrate how to effectively use the element-wise logical operators to select rows in a Pandas DataFrame based on these multiple conditions:
🌐
Kanoki
kanoki.org › 2020 › 01 › 21 › pandas-dataframe-filter-with-multiple-conditions
Pandas dataframe filter with Multiple conditions | kanoki
January 21, 2020 - In this post we have seen that what are the different methods which are available in the Pandas library to filter the rows and get a subset of the dataframe · And how these functions works: loc works with column labels and indexes, whereas eval and query works only with columns and boolean indexing works with values in a column only · Let me know your thoughts in the comments section below if you find this helpful or knows of any other functions which can be used to filter rows of dataframe using multiple conditions
🌐
Like Geeks
likegeeks.com › home › python › pandas › filter using pandas query method with multiple conditions
Filter Using Pandas query method with multiple conditions
You can use parentheses to dictate the order in which conditions are evaluated in the query method. Suppose you want to select records where either the Age is greater than 24 and the Salary is less than 70000, or the Age is less than 25. filtered_df = df.query("(Age > 24 & Salary < 70000) | (Age < 25)") print(filtered_df) ... Here, parentheses make it clear that the AND operation (&) should be carried out before the OR operation (|). In the context of Pandas query method, you can integrate regex to add another dimension to your data filtering capabilities.
🌐
Net Informations
net-informations.com › ds › pd › mcolumns.htm
Selecting multiple columns in a Pandas dataframe based on condition
This function is particularly useful for filtering data based on specific criteria and identifying rows that match certain conditions. ... The isin() method in Pandas enables the selection of multiple columns from a DataFrame based on specific conditional values.
🌐
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 - To filter rows based on multiple conditions, we can use the & (and) and | (or) operators to combine multiple conditions. For example, let’s say we have a dataframe df with columns A, B, and C. We want to select all rows where A is greater ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › selecting-rows-in-pandas-dataframe-based-on-conditions
Selecting rows in pandas DataFrame based on conditions - GeeksforGeeks
October 30, 2025 - Note: Always use parentheses around each condition. ... Age >= 20 and Stream in @options selects rows where Age ≥ 20 and Stream is in the Python list options.
🌐
Easy Tweaks
easytweaks.com › pandas-select-filter-columns-multiple-conditions
How to filter rows by multiple conditions in Pandas?
August 23, 2022 - Master meetings, chats, channels and online collaboration · Go beyond the basics in Word, Excel, PowerPoint and Outlook