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 - For a DataFrame, specifying a list or Series of boolean values (True or False) in [] will extract the rows corresponding to True. mask = [True, False, True, False, True, False] print(df[mask]) # name age state point # 0 Alice 24 NY 64 # 2 Charlie ...
🌐
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 - 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 data dataFrame = pd.DataFrame({'Name': [' RACHEL ', ...
🌐
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.
🌐
KeyToDataScience
keytodatascience.com › data science › selecting rows and columns based on conditions in python pandas dataframe
Selecting Rows and Columns Based on Conditions in Python Pandas DataFrame - KeyToDataScience
January 16, 2022 - You can update values in columns applying different conditions. For example, we will update the degree of persons whose age is greater than 28 to “PhD”. # select the rows where age is greater than 28 df.loc[df['age'] > 28, "degree"] = "PhD"
🌐
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.
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
Find elsewhere
🌐
BTech Geeks
btechgeeks.com › home › pandas select rows by multiple conditions – python pandas : select rows in dataframe by conditions on multiple columns
Pandas select rows by multiple conditions - Python Pandas : Select Rows in DataFrame by conditions on multiple columns - BTech Geeks
July 25, 2024 - Therefore, it will return a DataFrame in which Column ‘Product‘ contains either ‘Pen‘ or ‘Pencil‘ only i.e. ... RESTART: C:/Users/HP/Desktop/dataframe.py Name Product Sale 1 ankur pencil 28 2 Rekha pen 30 5 Mayank pencil 30 · In this method we are going to select rows in above example for which ‘Sale’ column contains value greater than 20 & less than 33.So for this we are going to give some condition. import pandas as pd students = [ ('Shyam', 'books' , 24) , ('ankur', 'pencil' , 28) , ('Rekha', 'pen' , 30) , ('Sarika', 'books', 62) , ('Lata', 'file' , 33) , ('Mayank', 'pencil' , 30) ] dataframeobj = pd.DataFrame(students, columns = ['Name' , 'Product', 'Sale']) filterinfDataframe = dataframeobj[(dataframeobj['Sale'] > 20) & (dataframeobj['Sale'] < 33) ] print(filterinfDataframe)
🌐
GeeksforGeeks
geeksforgeeks.org › selecting-rows-in-pandas-dataframe-based-on-conditions
Selecting rows in pandas DataFrame based on conditions - GeeksforGeeks
August 7, 2024 - In this article, we will learn how to select the limited rows with given columns with the help of these methods. Example 1: Select two columns Python3 # Import pandas package import pandas as pd # Defi · 2 min read Drop rows from dataframe based on certain condition applied on a column - Pandas
🌐
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 ...
🌐
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
🌐
Data Science Discovery
discovery.cs.illinois.edu › guides › DataFrame-Row-Selection › selecting-rows-dataframe
Select Rows From A DataFrame - Data Science Discovery
It is possible to select rows that meet different criteria using multiple conditions by joining conditionals together with & (AND) or | (OR) logical operators. (Note: Python requires the use of parentheses around the conditionals when using ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas select rows based on column values
Pandas Select Rows Based on Column Values - Spark By {Examples}
June 12, 2025 - In pandas, you can select rows based on column values using boolean indexing or using methods like DataFrame.loc[] attribute, DataFrame.query(), or
🌐
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
Fare Cabin Embarked 1 2 1 1 ... 71.2833 C85 C 6 7 0 1 ... 51.8625 E46 S 11 12 1 1 ... 26.5500 C103 S 13 14 0 3 ... 31.2750 NaN S 15 16 1 2 ... 16.0000 NaN S [5 rows x 12 columns] To select rows based on a conditional expression, use a condition inside the selection brackets [].
🌐
Python Examples
pythonexamples.org › pandas-dataframe-select-rows-by-condition
Pandas DataFrame - Select rows by condition
In Pandas DataFrame, you can select rows by a condition using boolean indexing. The condition could be based on a single column or multiple columns.
🌐
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.
🌐
Statology
statology.org › home › pandas: how to select rows based on column values
Pandas: How to Select Rows Based on Column Values
March 25, 2025 - You’re right that this is a common need when working with pandas. To select rows where a string column equals one of multiple values (like team “B” OR “C”), the `.isin()` method actually works perfectly.
🌐
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.