You can use all with boolean indexing:

print ((df == 'something1').all(1))
0     True
1    False
2     True
3    False
4    False
dtype: bool

print (df[(df == 'something1').all(1)])
         col1        col2
0  something1  something1
2  something1  something1

EDIT:

If need select only some columns you can use isin with boolean indexing for selecting desired columns and then use subset - df[cols]:

print (df)
         col1        col2 col3
0  something1  something1    a
1  something2  something3    s
2  something1  something1    r
3  something2  something3    a
4  something1  something2    a

cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')

print (df[(df[cols] == 'something1').all(1)])
         col1        col2 col3
0  something1  something1    a
2  something1  something1    r
Answer from jezrael on Stack Overflow
🌐
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 [].
Discussions

Selecting Columns Based on Condition
If you don't want to do numeric_only because other columns also contain numbers, you can filter columns by name. As all the relevant columns have % in you can do df['total'] = df.filter(regex="%").sum(axis=1) Or you can pass in a list of column names to filter instead if you can't regex it. cols = ["Col 1 %", "Col 2 %", "Col 3 %"] df['total'] = df[cols].sum(axis=1) More on reddit.com
🌐 r/learnpython
4
1
August 7, 2023
Pandas Python, select columns based on rows conditions - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Making the OWASP top ten in the vibe code... ... Help Shape the 2026 Developer Survey! 4250 How can I iterate over rows in a Pandas DataFrame? 3634 How do I select rows from a DataFrame based on column ... More on stackoverflow.com
🌐 stackoverflow.com
Filtering a pandas float column by “less than”
Can also do df = df.query(“column_name < 100.0”) IMO this is never a bad option since it’s extremely concise and clear. Anyone familiar with SQL, excel, etc will immediately understand what they’re looking at. More on reddit.com
🌐 r/learnpython
5
1
March 26, 2020
How do I count the number of values greater than > 3 for every column in a Pandas DataFrame?
make some fake data: import pandas as pd d1 = {'S1' : [10,20,30,40,50], 'S2' : [100,200,300,400,500]} df = pd.DataFrame(d1) a filter for column 1 print(df[df.S1>20]) S1 S2 2 30 300 3 40 400 4 50 500 a filter for column 2 print(df[df.S2<200]) S1 S2 0 10 100 You can combine with & and | print(df[(df.S1>10) & (df.S2<300)]) S1 S2 1 20 200 add .count() if you just want the counts. More on reddit.com
🌐 r/learnpython
1
1
January 7, 2024
🌐
Statology
statology.org › home › pandas: how to select columns based on condition
Pandas: How to Select Columns Based on Condition
November 4, 2022 - Method 3: Select Columns Where At Least One Row Meets Multiple Conditions · #select columns where at least one row has a value between 10 and 15 df.loc[:, ((df>=10) & (df<=15)).any()] The following examples show how to use each method with the following pandas DataFrame:
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › select pandas columns based on condition
Select Pandas Columns Based on Condition - Spark By {Examples}
June 27, 2025 - We can select columns based on single/multiple conditions using the pandas loc[] attribute. The DataFrame.loc[] attribute property is used to select rows
🌐
GeeksforGeeks
geeksforgeeks.org › python › selecting-rows-in-pandas-dataframe-based-on-conditions
Selecting rows in pandas DataFrame based on conditions - GeeksforGeeks
October 30, 2025 - You can filter rows based on whether a column’s value exists in a list. ... Returns a boolean Series used to filter rows. ... 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.
🌐
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.
🌐
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 - Select rows or columns in Pandas DataFrame based on various conditions using .loc, .iloc and conditional operators '>', '=', '!' With Examples and Code.
Find elsewhere
🌐
YouTube
youtube.com › watch
How to Select Columns Based on a Logical Condition in Pandas (Python) - YouTube
↓ Code Available Below! ↓ This video shows how to select columns of a data frame based on a logical condition. Filtering or subsetting the columns of a dat...
Published: October 29, 2020
🌐
Medium
medium.com › @heyamit10 › how-to-filter-columns-based-on-conditions-5f4c41dac45b
How to Filter Columns Based on Conditions? | by Hey Amit | Medium
March 6, 2025 - Instead of hardcoding, just pass the list to pandas.” · columns_to_keep = ['Name', 'Salary'] df_filtered = df[columns_to_keep] print(df_filtered) ... If you’re working with different datasets but need consistent columns, just update the list. No need for repetitive manual selection.
🌐
Codegive
codegive.com › blog › pandas_select_columns_by_condition.php
Master Pandas Column Selection: Unleash Data Insights by Condition in 2026!
A: df.filter() is a specialized method optimized for selecting columns (or rows) based on labels (names) using like (substring) or regex (regular expression) patterns. df.loc is a general label-based indexer that, when combined with a boolean array derived from df.columns or df.dtypes, offers maximum flexibility to select columns by condition using arbitrary logic. For simple name patterns, filter is often more concise; for complex or data-dependent conditions, loc with boolean indexing is necessary. Q: How do I select columns that contain no missing values in pandas?
🌐
Reddit
reddit.com › r/learnpython › selecting columns based on condition
r/learnpython on Reddit: Selecting Columns Based on Condition
August 7, 2023 -

Hey, everyone! This should be super easy and I think I'm having Monday-brain, but I'm having trouble trying to trying to do something to rows based on particular conditions. Here's some pseudo data:

d = {'Col 1': ['a', 'b'], 'Col 1 %': [1, 2], 'Col 2': ['c', 'd'], 'Col 2 %': [3, 4], 'Col 3': ['e', 'f']'Col 3 %': [2, 4]}
fake_df = pd.DataFrame(data=d)
fake_df

The df looks like...

	Col 1	Col 1 %	Col 2	Col 2 %	Col 3	Col 3 %
0	a	1	c	3	e	2
1	b	2	d	4	f	4

Now let's say I want to do a sum across all rows. Now it won't work by doing sum because not all of these are ints/floats. What I'm currently doing to do this is:

fake_df['Total'] = fake_df.columns[:-1] == "%".sum()

The error is:

AttributeError: 'str' object has no attribute 'sum'

Now I get it. I know it's not working, but Google isn't really too kind because when I search for the answer, it's thinking I'm searching based on some of the values within the data, not the columns. My intended solution is something like:

    Total
0    6
1    10

Now for the hell of it, I tried:

fake_df['Total'] = fake_df.sum()

Which, as expected, resulted in:

Col 1	Col 1 %	Col 2	Col 2 %	Col 3	Col 3 %	Total
0	a	1	c	3	e	2	NaN
1	b	2	d	4	f	4	NaN

I'm going to ask that the response not use any argument within the sum function to skip that because in my actual df problem, I only want to use rows that end in "%", and not all numeric-only columns end in that.

Thanks!

🌐
Codepointtech
codepointtech.com › home › how to select columns based on condition in pandas
How to Select Columns Based on Condition in Pandas - codepointtech.com
January 17, 2026 - One of the most common conditions for column selection is their data type. Pandas provides the convenient select_dtypes() method for this purpose.
🌐
Towards Data Science
towardsdatascience.com › home › latest › interesting ways to select pandas dataframe columns
Interesting Ways to Select Pandas DataFrame Columns | Towards Data Science
April 16, 2021 - Here, if the mean of all the values in a column meet a condition, return the column. ... Thanks for checking this out and feel free to reference it often. ... A review on MIT Open Courseware - 6.0001: Introduction to Computer Science and Programming in Python (with my notes shared) ... An overview of Python's alternative to loops and why you should use them. ... A step-by-step guide to understanding distributed data, lazy logic, and your first DataFrame.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › pandas-select-columns
Pandas Select Columns - GeeksforGeeks
July 23, 2025 - This is helpful when you know the position of the columns rather than their names. ... The filter() method is useful when you want to select columns based on certain conditions, such as column names that match a specific pattern.
🌐
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 - This approach allows you to filter ... complex conditions that depend on the data itself. You can apply custom functions using lambda expressions to implement sophisticated filtering logic. When working with pandas DataFrames, selecting and filtering rows based on column values is ...
🌐
datagy
datagy.io › home › pandas tutorials › viewing and selecting data in pandas › selecting columns in pandas: complete guide
Selecting Columns in Pandas: Complete Guide • datagy
December 15, 2022 - In this tutorial, you’ll learn how to select all the different ways you can select columns in Pandas, either by name or index. You’ll learn how to use the loc, iloc accessors and how to select columns directly. You’ll also learn how to select columns conditionally, such as those containing a specific substring.
🌐
Altcademy
altcademy.com › blog › how-to-select-certain-columns-in-pandas
How to select certain columns in Pandas - Altcademy.com
January 16, 2024 - In this example, df.mean() > 1 creates a boolean mask, where only columns satisfying the condition have True values. loc then uses this mask to select the appropriate columns. Let's use an analogy to reinforce what we've learned. Imagine a bookshelf representing your DataFrame. Each book on the shelf is a column. Using square brackets to select a column is like picking a specific book by its title.
🌐
Medium
medium.com › @arsalan_zafar › conditional-selection-in-pandas-f4a840960769
Conditional selection in Pandas. Part 5 — Pandas What Why How | by Arsalan Zafar | Medium
January 4, 2022 - We can also pass conditions with the loc and iloc methods, which is a more convenient way to filter out the data because we can get only those rows & columns we are interested. This time we create the same condition but assign it in a variable (high_salary_filter) that increases the readability, and our code looks better. ... Now we will pass our condition to the loc method and select only three columns — Country , ConvertedComp, LanguageWorkedWith
🌐
APXML
apxml.com › courses › essential-numpy-pandas › chapter-7-data-selection-indexing-pandas › conditional-selection
Conditional Selection (Boolean Indexing)
We can apply a comparison directly to the 'Age' column: ... As you can see, Pandas performs the comparison (> 25) element-wise for the entire 'Age' Series, resulting in a new Series of boolean values. True indicates the condition is met for that row, and False indicates it is not. This boolean Series acts like a filter. You can pass it directly inside the square brackets [] of the DataFrame (or .loc) to select ...
Top answer
1 of 3
8

Use gt and any to filter the df:

In [287]:
df.ix[:,df.gt(2).any()]

Out[287]:
          2
0  1.590124
1  2.500397

Here we use ix to select all rows, the first : and the next arg is a boolean mask of the columns that meet the condition:

In [288]:
df.gt(2)

Out[288]:
       0      1      2      3
0  False  False  False  False
1  False  False   True  False

In [289]:
df.gt(2).any()

Out[289]:
0    False
1    False
2     True
3    False
dtype: bool

In your example what you did was select the cell value for the first row and second column, you then tried to use this to mask the columns but this just returned the first column hence why it didn't work:

In [291]:
df.iloc[(0,1)]

Out[291]:
1.3296030000000001

In [293]:
df.columns[df.iloc[(0,1)]>2]

Out[293]:
'0'
2 of 3
3

Use mask created with df > 2 with any and then select columns by ix:

import pandas as pd
np.random.seed(18)
df = pd.DataFrame(np.random.randn(2, 4))
print(df)
          0         1         2         3
0  0.079428  2.190202 -0.134892  0.160518
1  0.442698  0.623391  1.008903  0.394249

print ((df>2).any())
0    False
1     True
2    False
3    False
dtype: bool

print (df.ix[:, (df>2).any()])
          1
0  2.190202
1  0.623391

EDIT by comment:

You can check your solution per partes:

It seems it works, but it always select second column (1, python count from 0) column if condition True:

print (df.iloc[(0,1)])
2.19020235741

print (df.iloc[(0,1)] > 2)
True

print (df.columns[df.iloc[(0,1)]>2])
1

print (df[df.columns[df.iloc[(0,1)]>2]])
0    2.190202
1    0.623391
Name: 1, dtype: float64

And first column (0) column if False, because boolean True and False are casted to 1 and 0:

np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
print (df)
          0         1         2         3
0 -0.312328  0.339285 -0.155909 -0.501790
1  0.235569 -1.763605 -1.095862 -1.087766

print (df.iloc[(0,1)])
0.339284706046

print (df.iloc[(0,1)] > 2)
False

print (df.columns[df.iloc[(0,1)]>2])
0

print (df[df.columns[df.iloc[(0,1)]>2]])
0   -0.312328
1    0.235569
Name: 0, dtype: float64

If change column names:

np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
df.columns = ['a','b','c','d']
print (df)
          a         b         c         d
0 -0.312328  0.339285 -0.155909 -0.501790
1  0.235569 -1.763605 -1.095862 -1.087766

print (df.iloc[(0,1)] > 2)
False

print (df[df.columns[df.iloc[(0,1)]>2]])
0   -0.312328
1    0.235569
Name: a, dtype: float64