To select rows whose column value equals a scalar, some_value, use ==:

df.loc[df['column_name'] == some_value]

To select rows whose column value is in an iterable, some_values, use isin:

df.loc[df['column_name'].isin(some_values)]

Combine multiple conditions with &:

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]

Note the parentheses. Due to Python's operator precedence rules, & binds more tightly than <= and >=. Thus, the parentheses in the last example are necessary. Without the parentheses

df['column_name'] >= A & df['column_name'] <= B

is parsed as

df['column_name'] >= (A & df['column_name']) <= B

which results in a Truth value of a Series is ambiguous error.


To select rows whose column value does not equal some_value, use !=:

df.loc[df['column_name'] != some_value]

The isin returns a boolean Series, so to select rows whose value is not in some_values, negate the boolean Series using ~:

df = df.loc[~df['column_name'].isin(some_values)] # .loc is not in-place replacement

For example,

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

print(df.loc[df['A'] == 'foo'])

yields

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use isin:

print(df.loc[df['B'].isin(['one','three'])])

yields

     A      B  C   D
0  foo    one  0   0
1  bar    one  1   2
3  bar  three  3   6
6  foo    one  6  12
7  foo  three  7  14

Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use df.loc:

df = df.set_index(['B'])
print(df.loc['one'])

yields

       A  C   D
B              
one  foo  0   0
one  bar  1   2
one  foo  6  12

or, to include multiple values from the index use df.index.isin:

df.loc[df.index.isin(['one','two'])]

yields

       A  C   D
B              
one  foo  0   0
one  bar  1   2
two  foo  2   4
two  foo  4   8
two  bar  5  10
one  foo  6  12
Answer from unutbu on Stack Overflow
🌐
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)
Top answer
1 of 16
6655

To select rows whose column value equals a scalar, some_value, use ==:

df.loc[df['column_name'] == some_value]

To select rows whose column value is in an iterable, some_values, use isin:

df.loc[df['column_name'].isin(some_values)]

Combine multiple conditions with &:

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]

Note the parentheses. Due to Python's operator precedence rules, & binds more tightly than <= and >=. Thus, the parentheses in the last example are necessary. Without the parentheses

df['column_name'] >= A & df['column_name'] <= B

is parsed as

df['column_name'] >= (A & df['column_name']) <= B

which results in a Truth value of a Series is ambiguous error.


To select rows whose column value does not equal some_value, use !=:

df.loc[df['column_name'] != some_value]

The isin returns a boolean Series, so to select rows whose value is not in some_values, negate the boolean Series using ~:

df = df.loc[~df['column_name'].isin(some_values)] # .loc is not in-place replacement

For example,

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

print(df.loc[df['A'] == 'foo'])

yields

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use isin:

print(df.loc[df['B'].isin(['one','three'])])

yields

     A      B  C   D
0  foo    one  0   0
1  bar    one  1   2
3  bar  three  3   6
6  foo    one  6  12
7  foo  three  7  14

Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use df.loc:

df = df.set_index(['B'])
print(df.loc['one'])

yields

       A  C   D
B              
one  foo  0   0
one  bar  1   2
one  foo  6  12

or, to include multiple values from the index use df.index.isin:

df.loc[df.index.isin(['one','two'])]

yields

       A  C   D
B              
one  foo  0   0
one  bar  1   2
two  foo  2   4
two  foo  4   8
two  bar  5  10
one  foo  6  12
2 of 16
854

There are several ways to select rows from a Pandas dataframe:

  1. Boolean indexing (df[df['col'] == value] )
  2. Positional indexing (df.iloc[...])
  3. Label indexing (df.xs(...))
  4. df.query(...) API

Below I show you examples of each, with advice when to use certain techniques. Assume our criterion is column 'A' == 'foo'

(Note on performance: For each base type, we can keep things simple by using the Pandas API or we can venture outside the API, usually into NumPy, and speed things up.)


Setup

The first thing we'll need is to identify a condition that will act as our criterion for selecting rows. We'll start with the OP's case column_name == some_value, and include some other common use cases.

Borrowing from @unutbu:

import pandas as pd, numpy as np

df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})

1. Boolean indexing

... Boolean indexing requires finding the true value of each row's 'A' column being equal to 'foo', then using those truth values to identify which rows to keep. Typically, we'd name this series, an array of truth values, mask. We'll do so here as well.

mask = df['A'] == 'foo'

We can then use this mask to slice or index the data frame

df[mask]

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

This is one of the simplest ways to accomplish this task and if performance or intuitiveness isn't an issue, this should be your chosen method. However, if performance is a concern, then you might want to consider an alternative way of creating the mask.


2. Positional indexing

Positional indexing (df.iloc[...]) has its use cases, but this isn't one of them. In order to identify where to slice, we first need to perform the same boolean analysis we did above. This leaves us performing one extra step to accomplish the same task.

mask = df['A'] == 'foo'
pos = np.flatnonzero(mask)
df.iloc[pos]

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

3. Label indexing

Label indexing can be very handy, but in this case, we are again doing more work for no benefit

df.set_index('A', append=True, drop=False).xs('foo', level=1)

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

4. df.query() API

pd.DataFrame.query is a very elegant/intuitive way to perform this task, but is often slower. However, if you pay attention to the timings below, for large data, the query is very efficient. More so than the standard approach and of similar magnitude as my best suggestion.

df.query('A == "foo"')

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

My preference is to use the Boolean mask

Actual improvements can be made by modifying how we create our Boolean mask.

mask alternative 1 Use the underlying NumPy array and forgo the overhead of creating another pd.Series

mask = df['A'].values == 'foo'

I'll show more complete time tests at the end, but just take a look at the performance gains we get using the sample data frame. First, we look at the difference in creating the mask

%timeit mask = df['A'].values == 'foo'
%timeit mask = df['A'] == 'foo'

5.84 µs ± 195 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
166 µs ± 4.45 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Evaluating the mask with the NumPy array is ~ 30 times faster. This is partly due to NumPy evaluation often being faster. It is also partly due to the lack of overhead necessary to build an index and a corresponding pd.Series object.

Next, we'll look at the timing for slicing with one mask versus the other.

mask = df['A'].values == 'foo'
%timeit df[mask]
mask = df['A'] == 'foo'
%timeit df[mask]

219 µs ± 12.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
239 µs ± 7.03 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

The performance gains aren't as pronounced. We'll see if this holds up over more robust testing.


mask alternative 2 We could have reconstructed the data frame as well. There is a big caveat when reconstructing a dataframe—you must take care of the dtypes when doing so!

Instead of df[mask] we will do this

pd.DataFrame(df.values[mask], df.index[mask], df.columns).astype(df.dtypes)

If the data frame is of mixed type, which our example is, then when we get df.values the resulting array is of dtype object and consequently, all columns of the new data frame will be of dtype object. Thus requiring the astype(df.dtypes) and killing any potential performance gains.

%timeit df[m]
%timeit pd.DataFrame(df.values[mask], df.index[mask], df.columns).astype(df.dtypes)

216 µs ± 10.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.43 ms ± 39.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

However, if the data frame is not of mixed type, this is a very useful way to do it.

Given

np.random.seed([3,1415])
d1 = pd.DataFrame(np.random.randint(10, size=(10, 5)), columns=list('ABCDE'))

d1

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
2  0  2  0  4  9
3  7  3  2  4  3
4  3  6  7  7  4
5  5  3  7  5  9
6  8  7  6  4  7
7  6  2  6  6  5
8  2  8  7  5  8
9  4  7  6  1  5

%%timeit
mask = d1['A'].values == 7
d1[mask]

179 µs ± 8.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Versus

%%timeit
mask = d1['A'].values == 7
pd.DataFrame(d1.values[mask], d1.index[mask], d1.columns)

87 µs ± 5.12 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

We cut the time in half.


mask alternative 3

@unutbu also shows us how to use pd.Series.isin to account for each element of df['A'] being in a set of values. This evaluates to the same thing if our set of values is a set of one value, namely 'foo'. But it also generalizes to include larger sets of values if needed. Turns out, this is still pretty fast even though it is a more general solution. The only real loss is in intuitiveness for those not familiar with the concept.

mask = df['A'].isin(['foo'])
df[mask]

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

However, as before, we can utilize NumPy to improve performance while sacrificing virtually nothing. We'll use np.in1d

mask = np.in1d(df['A'].values, ['foo'])
df[mask]

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

Timing

I'll include other concepts mentioned in other posts as well for reference.

Code Below

Each column in this table represents a different length data frame over which we test each function. Each column shows relative time taken, with the fastest function given a base index of 1.0.

res.div(res.min())

                         10        30        100       300       1000      3000      10000     30000
mask_standard         2.156872  1.850663  2.034149  2.166312  2.164541  3.090372  2.981326  3.131151
mask_standard_loc     1.879035  1.782366  1.988823  2.338112  2.361391  3.036131  2.998112  2.990103
mask_with_values      1.010166  1.000000  1.005113  1.026363  1.028698  1.293741  1.007824  1.016919
mask_with_values_loc  1.196843  1.300228  1.000000  1.000000  1.038989  1.219233  1.037020  1.000000
query                 4.997304  4.765554  5.934096  4.500559  2.997924  2.397013  1.680447  1.398190
xs_label              4.124597  4.272363  5.596152  4.295331  4.676591  5.710680  6.032809  8.950255
mask_with_isin        1.674055  1.679935  1.847972  1.724183  1.345111  1.405231  1.253554  1.264760
mask_with_in1d        1.000000  1.083807  
Discussions

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
Filter Pandas Dataframe Unnamed column on Multiple values

Hi, I am not a huge fan of the isin() syntax. There is actually a way to do this using .query() but given that your DataFrame does not have column names it is not as elegant.

filter = ["H", "W"]
df2 = pd.DataFrame({"col": df.iloc[:, 31]})
df2 = df2.query("col in @filter")
More on reddit.com
🌐 r/learnpython
3
1
December 20, 2019
Filter row based on maximum value of some column
You can index based on a condition. df.B .== maximum(df.B) gives you a bitvector of length 5, and Julia accepts a bitvector as an index. Therefore, this should give you what you want using DataFrame df = DataFrame(A = [10, 20, 30, 40, 50], B = [5, 15, 25, 35, 45]) df[df.B .== maximum(df.B), :] # This works for me More on reddit.com
🌐 r/Julia
6
4
August 14, 2023
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.6 documentation
>>> # select columns by regular expression >>> df.filter(regex="e$", axis=1) one three mouse 1 3 rabbit 4 6
🌐
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 - You pick the column and match it with the value you want. A common confusion when it comes to filtering in Pandas is the use of conditional operators. Python syntax creates trouble for many. Note: In Pandas, and is replaced with & , or is replaced with | and not is replaced with ~ I find out that Madrid is the top-ranking city in terms of revenue. I’d like to compare the sales details of Madrid against all the other cities. This can be achieved by assigning conditions to variables.
🌐
Medium
deallen7.medium.com › using-pandas-contains-method-to-filter-a-dataframe-column-for-specific-words-or-phrases-7567e7dcebb8
Using Pandas’ contains() method to filter a DataFrame column for specific words or phrases | by David Allen | Medium
July 26, 2022 - If you have ever analyzed text data in an Excel Spreadsheet or a Google Sheet, you’re familiar with the task of filtering a column of text for a word or phrase. Pandas’ contains() method gives you this same ability for any column of string ...
🌐
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 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S [5 rows x 12 columns] The notna() conditional function returns a True for each row the values are not a Null value. As such, this can be combined with the selection brackets [] to filter the data table.
Find elsewhere
🌐
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.
🌐
Towards Data Science
towardsdatascience.com › home › latest › stop writing messy boolean masks: 10 elegant ways to filter pandas dataframes
Stop Writing Messy Boolean Masks: 10 Elegant Ways to Filter Pandas DataFrames | Towards Data Science
January 21, 2026 - Similar to our output above, we’re ... False values. Let’s fix it up real quick. And there we go! Filtering by date is straightforward. For instance. ... # Filter by date condition# Example: Orders placed after “2023–01–08”df_sales[df_sales[“OrderDate”] > “2023–01–08”] This checks for orders placed after January 8, 2023. And here’s the output. The cool thing about Pandas is that it ...
🌐
Educative
educative.io › answers › how-to-filter-pandas-dataframe-by-column-value
How to filter pandas DataFrame by column value
... isin() method: We can filter rows of a DataFrame based on whether the values in a specified column are present in a given list or array. ... To learn how we can apply a filter on the column values, let's first create a data example.
🌐
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!

🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas filter by column value
Pandas Filter by Column Value - Spark By {Examples}
June 6, 2025 - Pandas support several ways to filter by column value, DataFrame.query() function is the most used to filter rows based on a specified expression,
🌐
SheCanCode
shecancode.io › home › news & articles › filter a dataframe by partial string or pattern
Filter a DataFrame by Partial String or Pattern - SheCanCode
March 4, 2025 - As a data professional, chances are you will often need to separate data based on its contents. In this article, we looked at 8 ways to filter a DataFrame by the string values present in the columns. We used Pandas, Lambda functions, and the ‘in’ keyword.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.5 documentation
>>> # select columns by regular expression >>> df.filter(regex="e$", axis=1) one three mouse 1 3 rabbit 4 6
🌐
YoungWonks
youngwonks.com › blog › top-10-ways-to-filter-pandas-dataframe
What is pandas and a pandas dataframe? What are the top 10 ways to filter pandas dataframe? Read our blog to learn more...
June 17, 2022 - Our blog tells you how to create a pandas dataframe and use the top 10 ways to filter pandas dataframe. It includes a Python source code.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Filter rows/columns by labels with filter() | note.nkmk.me
January 24, 2024 - In pandas, use the filter() method to select rows or columns in the DataFrame based on their labels (names). This method is provided for both DataFrame and Series. pandas.DataFrame.filter — pandas 2. ...
🌐
W3Schools
w3schools.com › python › pandas › pandas_analyzing.asp
Pandas - Analyzing DataFrames
Pandas HOME Pandas Intro Pandas Getting Started Pandas Series Pandas DataFrames Pandas Read CSV Pandas Read JSON Pandas Analyzing Data · Cleaning Data Cleaning Empty Cells Cleaning Wrong Format Cleaning Wrong Data Removing Duplicates ... One of the most used method for getting a quick overview of the DataFrame, is the head() method. The head() method returns the headers and a specified number of rows, starting from the top. Get a quick overview by printing the first 10 rows of the DataFrame:
🌐
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 DataFrame based on a list of values in Pandas: Use .isin() on the column with your list to create a boolean mask. Apply the mask to the DataFrame using boolean indexing to get the filtered result.
🌐
Medium
medium.com › @amit25173 › filtering-data-in-pandas-basics-you-need-to-know-639ed999821b
Filtering Data in Pandas — Basics You Need to Know | by Amit Yadav | Medium
April 13, 2025 - Let’s step up your filtering game with some powerful Pandas tricks. 1. Filtering with isin() (Multiple Values in a Column)
🌐
Saturn Cloud
saturncloud.io › blog › how-to-filter-pandas-dataframe-by-substring-criteria
How to Filter Pandas DataFrame by Substring Criteria | Saturn Cloud Blog
May 1, 2026 - It provides data structures for efficiently storing and manipulating large datasets, as well as a wide range of functions for data analysis tasks such as filtering, aggregating, and transforming data. Suppose we have a pandas DataFrame with a column containing strings, and we want to filter this DataFrame to include only rows where the string in this column contains a specific substring.
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-pandas-dataframe-by-substring-criteria
Filter pandas DataFrame by substring criteria - GeeksforGeeks
July 23, 2025 - To filter a Pandas DataFrame using a substring in any specific column data, you can use one of several methods, including the .loc[], .query(), .filter(), .isin(), .apply(), and .map() methods.