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
Answer from DSM 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
A pandas Series is 1-dimensional and only the number of rows is returned. I’m interested in the age and sex of the Titanic passengers. In [8]: age_sex = titanic[["Age", "Sex"]] In [9]: age_sex.head() Out[9]: Age Sex 0 22.0 male 1 38.0 female 2 26.0 female 3 35.0 female 4 35.0 male · To select multiple columns, use a list of column names within the selection brackets [].
🌐
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.6 documentation
For production code, we recommended that you take advantage of the optimized pandas data access methods exposed in this chapter. See the MultiIndex / Advanced Indexing for MultiIndex and more advanced indexing documentation. See the cookbook for some advanced strategies. Object selection has had ...
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
python - Selecting specific rows from a pandas dataframe - Stack Overflow
I just want to know if there is any function in pandas that selects specific rows based on index from a dataframe without having to write your own function. For example: selecting rows with index ... More on stackoverflow.com
🌐 stackoverflow.com
Selecting rows in Pandas given condition a or condition b

Good idea to provide sample input - > output. Otherwise we may refer you to the same answers that are already not working for you.

Here's a snippet:

(df["B"] > 30) | (df["C"] == 700)

This will return rows where the B column is bigger than 30 or (|) C column equals 700.

More on reddit.com
🌐 r/learnpython
5
8
November 23, 2017
python - How to select a range of values in a pandas dataframe column? - Stack Overflow
import pandas as pd import numpy as np data = 'filename.csv' df = pd.DataFrame(data) df one two three four five a 0.469112 -0.282863 -1.509059 bar True b 0.932424 1.224... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.query.html
pandas.DataFrame.query — pandas 3.0.6 documentation
DataFrame.query(expr, *, parser='pandas', engine=None, local_dict=None, global_dict=None, resolvers=None, level=0, inplace=False)[source]#
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
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › indexing-and-selecting-data-with-pandas
Indexing and Selecting Data with Pandas - GeeksforGeeks
To select a single column, we simply refer the column name inside square brackets. Here we will be using NBA dataset which you can download it from here. ... import pandas as pd data = pd.read_csv("/content/nba.csv", index_col="Name") ...
Published: April 28, 2026
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.select_dtypes.html
pandas.DataFrame.select_dtypes — pandas 3.0.6 documentation
To select Pandas categorical dtypes, use 'category' To select Pandas datetimetz dtypes, use 'datetimetz' or 'datetime64[ns, tz]' Examples · >>> df = pd.DataFrame( ... {"a": [1, 2] * 3, "b": [True, False] * 3, "c": [1.0, 2.0] * 3} ... ) >>> df a b c 0 1 True 1.0 1 2 False 2.0 2 1 True 1.0 3 2 False 2.0 4 1 True 1.0 5 2 False 2.0 ·
Find elsewhere
🌐
Earth Data Science
earthdatascience.org › home
Select Data From Pandas Dataframes | Earth Data Science - Earth Lab
November 12, 2020 - This feature of pandas dataframes is very useful because you can create an index for pandas dataframes using a specific column (i.e. label) that you want to use for organizing and querying your data. For example, you can create an index from a specific column of values, and then use the attribute .loc to select data from the pandas dataframes using a value that is found in that index.
🌐
Medium
medium.com › @gis-ish › pandas-select-data-ca66d96049dd
Pandas — Select data
October 10, 2022 - The most basic method of selecting data is passing a the column or a list of columns through “[]” to select columns in that order.
🌐
Programiz
programiz.com › python-programming › pandas › select
Pandas Select (With Examples)
Pandas select refers to the process of extracting specific portions of data from a DataFrame.
🌐
Built In
builtin.com › data-science › pandas-filter
How to Filter Pandas DataFrames | Built In
Pandas is a highly efficient library on textual data as well. The functions and methods under the str accessor provide flexible ways to filter rows based on strings. For instance, we can select the names that start with the letter “J.”
Top answer
1 of 2
10

You could use either pandas.DataFrame.loc or pandas.DataFrame.iloc. See examples below.

import pandas as pd

d = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
     {'a': 100, 'b': 200, 'c': 300, 'd': 400},
     {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 },
     {'a': 1500, 'b': 2500, 'c': 3500, 'd': 4500}]

df = pd.DataFrame(d)

print(df)               # Print original dataframe
print(df.loc[1:2])      # Print rows with index 1 and 2, (method 1)
print(df.iloc[1:3])     # Print rows with index 1 and 2, (method 2)

Original dataframe: print(df) will print:

      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000
3  1500  2500  3500  4500

And print(df.loc[1:2]) for index selection by label:

      a     b     c     d
1   100   200   300   400
2  1000  2000  3000  4000

And print(df.iloc[1:3]) for row selection by integer. As mentioned by ALollz, rows are treated as numbers from 0 to len(df):

      a     b     c     d
1   100   200   300   400
2  1000  2000  3000  4000

A rule of thumb could be:

  • Use .loc when you want to refer to the actual value of the index, being a string or integer.

  • Use .iloc when you want to refer to the underlying row number which always ranges from 0 to len(df).

Note that the end value of the slice in .loc is included. This is not the case for .iloc, and for Python slices in general.

Pandas in general

Pandas has 'easy' ways of doing all sorts of stuff like this. If you have a problem that you think is common for manipulation of tabular data, try searching for pandas ways of getting it done before inventing it yourself. Pandas will almost always have a syntactically concise and computationally faster way of doing things than what we can write ourselves.

2 of 2
1

Use this:

rowData = your_df.loc[ 'index' , : ]
🌐
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 - How can I select specific columns after filtering? Once you filter rows, you can simply chain the column selection: df[df['Age'] > 25]['Name']. Now that you know how to pandas select rows by condition, you’re well on your way to mastering data manipulation in Python.
🌐
Panda Express
pandaexpress.com › gift-cards › select
Select a Gift Card | Panda Express | A Fast Casual Chinese Restaurant | Panda Express Chinese Restaurant
Earn Panda Points® with every qualifying purchase, get exclusive offers & rewards, and save & reorder your favorites for a fast checkout experience! Sign Up Log In · Digital Gift Physical Gift · All Cards · All Cards · Select this card · Digital Card ·
🌐
Kaggle
kaggle.com › code › residentmario › indexing-selecting-assigning
Indexing, Selecting & Assigning
April 21, 2023 - IntroductionNative accessorsIndexing in pandasManipulating the indexConditional selectionAssigning dataYour turn
🌐
DataCamp
datacamp.com › tutorial › python-select-columns
Python Pandas Select Columns Tutorial | DataCamp
November 25, 2024 - You can also use loc to select all rows but only a specific number of columns. Simply replace the first list that specifies the row labels with a colon. A slice going from beginning to end. This time, we get back all of the rows but only two columns. ... country capital BR Brazil Brasilia RU Russia Moscow IN India New Delhi CH China Beijing SA South Africa Pretoria · The iloc function allows you to subset pandas DataFrames based on their position or index.
🌐
Pandas
pandas.pydata.org › docs › user_guide › 10min.html
10 minutes to pandas — pandas 3.0.6 documentation
While standard Python / NumPy expressions for selecting and setting are intuitive and come in handy for interactive work, for production code, we recommend the optimized pandas data access methods, DataFrame.at(), DataFrame.iat(), DataFrame.loc() and DataFrame.iloc().
🌐
Towards Data Science
towardsdatascience.com › home › latest › modern dataframes in python: a hands-on tutorial with polars and duckdb
Modern DataFrames in Python: A Hands-On Tutorial with Polars and DuckDB | Towards Data Science
November 20, 2025 - This article explores modern alternatives to Pandas, including Polars and DuckDB, and examines how they can simplify and improve the handling of large datasets.