As you can see, the AND operator drops every row in which at least one value equals -1. On the other hand, the OR operator requires both values to be equal to -1 to drop them.

That's right. Remember that you're writing the condition in terms of what you want to keep, not in terms of what you want to drop. For df1:

df1 = df[(df.a != -1) & (df.b != -1)]

You're saying "keep the rows in which df.a isn't -1 and df.b isn't -1", which is the same as dropping every row in which at least one value is -1.

For df2:

df2 = df[(df.a != -1) | (df.b != -1)]

You're saying "keep the rows in which either df.a or df.b is not -1", which is the same as dropping rows where both values are -1.

PS: chained access like df['a'][1] = -1 can get you into trouble. It's better to get into the habit of using .loc and .iloc.

Answer from DSM 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

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
python - Pandas: np.where with multiple conditions on dataframes - Stack Overflow
hi folks i have look all over SO and google and cant find anything similar... I have a dataframe x (essentially consisting of one row and 300 columns) and another dataframe y with same size but 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
python - Pandas dataframe numpy where multiple conditions - Stack Overflow
Using pandas and numpy. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-pandas-dataframe-with-multiple-conditions
Filter Pandas Dataframe with multiple conditions - GeeksforGeeks
July 23, 2025 - The reason is dataframe may be having multiple columns and multiple rows. Selective display of columns with limited rows is always the expected view of users. To fulfill the user's expectations and also help in machine deep learning scenarios, ...
🌐
Data Science Parichay
datascienceparichay.com › article › pandas-filter-dataframe-for-multiple-conditions
Pandas - Filter DataFrame for multiple conditions
Two thumbs up - I recently switched to WPX Hosting and recommend their speed, service and security - they do know what they are talking about when it comes to WordPress hosting.
🌐
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 › @michalwesleymnach › the-complete-guide-to-create-columns-based-on-multiple-conditions-in-pandas-dataframes-eedf2c0392a6
The complete guide to creating columns based on multiple conditions in a Pandas DataFrame | by Michaël Ménaché | Medium
July 17, 2022 - Having worked with SAS for 13 years, I was a bit puzzled that Pandas doesn’t seem to have a simple syntax to create a column based on conditions such as “if sales > 30 and profit / sales > 30% then “good”, else if … then…”. This, for me, is most natural way to write such conditions: data table; set table; if sales > 50 then do; if profit / sales > 0.3 then rank = "A+" else rank = "A" end; else if sales > 20 then rank = "B" else if sales <= 20 then rank = "C" else rank = "ERR" run; But in Pandas, creating a column based on multiple conditions is not as straightforward:
🌐
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 ...
Find elsewhere
🌐
Like Geeks
likegeeks.com › home › python › pandas › filter using pandas query method with multiple conditions
Filter Using Pandas query method with multiple conditions
When you need to combine multiple conditions, the ampersand (&) serves as the logical AND operator. The syntax is straightforward: you specify each condition within a string, and separate them using &. ... import pandas as pd data = { 'ID': ...
🌐
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
🌐
YouTube
youtube.com › watch
How to Filter Data in Python Pandas with Multiple Conditions (Step-by-Step) - YouTube
🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automati...
Published: April 18, 2025
🌐
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.
🌐
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
🌐
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!

🌐
IncludeHelp
includehelp.com › python › numpy-where-function-multiple-conditions.aspx
Python - NumPy 'where' function multiple conditions
To tackle the problem of comparing two conditions only, we check the value with np.where() condition to check all the three conditions and assign the values to them. ... # Importing pandas package import pandas as pd # Import numpy package import numpy as np # Creating a Dictionary d = ...
🌐
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.
🌐
Saturn Cloud
saturncloud.io › blog › pandas-filtering-multiple-conditions
Pandas Filtering Multiple Conditions | Saturn Cloud Blog
May 1, 2026 - The most commonly used functions are loc and iloc. The loc function is used to filter data based on labels, while the iloc function is used to filter data based on integer positions.
🌐
Kanoki
kanoki.org › 2020 › 01 › 21 › pandas-dataframe-filter-with-multiple-conditions
Pandas dataframe filter with Multiple conditions | kanoki
January 21, 2020 - In this section we are going to see how to filter the rows of a dataframe with multiple conditions using these five methods · a) loc b) numpy where c) Query d) Boolean Indexing e) eval ... Get all rows having salary greater or equal to 100K and Age < 60 and Favourite Football Team Name starts with ‘S’ · loc is used to Access a group of rows and columns by label(s) or a boolean array · As an input to label you can give a single label or it’s index or a list of array of labels · Enter all the conditions and with & as a logical operator between them