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 - This article describes how to select rows of pandas.DataFrame by multiple conditions. Select rows by a certain condition Select rows by multiple conditionsThe &, |, and ~ operatorsThe isin() method Th ...
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 - Output resolves for the given conditions and finally, we are going to show only 2 columns namely Name and JOB. Here will get all rows having Salary greater or equal to 100000 and Age < 40 and their JOB starts with ‘D’ from the data frame. We need to use NumPy. ... # import module import pandas as pd import numpy as np # 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 filtered_values = np.where((dataFrame['Salary']>=100000) & (dataFrame['Age']< 40) & (dataFrame['JOB'].str.startswith('D'))) print(filtered_values) display(dataFrame.loc[filtered_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
Select rows from a DataFrame based on multiple conditions. ... import pandas as pd # Create a sample DataFrame data = {'Name': ['Teodosija', 'Sutton', 'Taneli', 'Ravshan', 'Ross'], 'Age': [26, 32, 25, 31, 28], 'Salary': [50000, 60000, 45000, 70000, 55000]} df = pd.DataFrame(data) # Select rows based on multiple conditions selected_rows = df[(df['Age'] > 25) & (df['Salary'] > 50000)] # Display the selected rows print(selected_rows)
🌐
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)')
🌐
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.
🌐
Kanoki
kanoki.org › 2020 › 01 › 21 › pandas-dataframe-filter-with-multiple-conditions
Pandas dataframe filter with Multiple conditions | kanoki
January 21, 2020 - The output from the np.where, which is a list of row index matching the multiple conditions is fed to dataframe loc function ... It is a standrad way to select the subset of data using the values in the dataframe and applying conditions on it
🌐
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 - As a data scientist or software engineer, you may often need to filter and manipulate data based on multiple conditions. Pandas, a popular Python library for data analysis, offers a powerful method called .loc that allows you to select rows and columns based on labels or boolean 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 - To implement selection based on multiple criteria, we leverage Boolean Algebra. In standard Python, we might use keywords like and or or. However, within the context of array-based computation utilized by Pandas (which relies heavily on NumPy ...
🌐
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.
🌐
Like Geeks
likegeeks.com › home › python › pandas › filter using pandas query method with multiple conditions
Filter Using Pandas query method with multiple conditions
The syntax is straightforward: you specify each condition within a string, and separate them using &. ... import pandas as pd data = { 'ID': [1, 2, 3, 4, 5], 'Name': ['John', 'Emily', 'Michael', 'Sarah', 'Jessica'], 'Age': [28, 24, 22, 25, 29], ...
🌐
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.
🌐
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 ...
🌐
Statology
statology.org › home › pandas: how to select columns based on condition
Pandas: How to Select Columns Based on Condition
November 4, 2022 - You can use the following methods to select columns in a pandas DataFrame by condition: Method 1: Select Columns Where At Least One Row Meets Condition · #select columns where at least one row has a value greater than 2 df.loc[:, (df > 2).any()] Method 2: Select Columns Where All Rows Meet Condition · #select columns where all rows have a value greater than 2 df.loc[:, (df > 2).all()] Method 3: Select Columns Where At Least One Row Meets Multiple Conditions ·