How do I select rows with several conditions in pandas?

Keep the rows that meet one or several conditions with df[…] and .loc, combine tests with &, | and ~, use .isin(), compare .loc with .iloc, and change a selection safely with .copy().

After this lesson, you can keep the rows of a table that meet several conditions and change a selection without changing the table by accident.

How do I keep the rows that meet a condition?

A comparison on a column gives one True or False per row, and prices[condition] keeps the rows with True. prices.loc[condition, columns] also picks columns, with the rows before the comma and the columns after it. More than 15,000 shares traded on rows 1, 3, 4 and 5.

The example is a long panel of three made-up stocks on two days, with closes in dollars and volumes in shares. > is strict, so a volume of exactly 15,000 would fail.

The following box builds the panel and keeps the busy rows.

import pandas as pd

prices = pd.DataFrame({
    "date": ["2026-03-02", "2026-03-03"] * 3,
    "ticker": ["AAA", "AAA", "BBB", "BBB", "CCC", "CCC"],
    "close": [101.2, 102.0, 55.1, 54.0, 230.0, 228.4],
    "volume": [12000, 18500, 8000, 21000, 15100, 16000],
})
busy = prices["volume"] > 15000
print(prices.loc[busy, ["ticker", "volume"]])
  ticker  volume
1    AAA   18500
3    BBB   21000
4    CCC   15100
5    CCC   16000

How do I combine conditions?

& keeps the rows that pass both tests, | those that pass at least one and ~ reverses a test. AAA with more than 15,000 shares is row 1 alone. .isin(["AAA", "CCC"]) tests whether each ticker is one of several, which replaces a chain of |.

Python runs & and | before == and >, so a comparison written inside a combination needs its own brackets. Without them, the selection raises a TypeError. A test stored under a name, such as busy, needs no brackets.

The following box combines the tests in three ways.

print(prices[(prices["ticker"] == "AAA") & busy])
print(prices.loc[prices["ticker"].isin(["AAA", "CCC"]) & ~busy, ["ticker", "volume"]])
         date ticker  close  volume
1  2026-03-03    AAA  102.0   18500
  ticker  volume
0    AAA   12000

How do .loc and .iloc differ?

.loc selects by row label and column name, and .iloc by position, counting from 0. A slice with .loc includes its end, so prices.loc[0:2] holds three rows, while prices.iloc[0:2] holds two.

After a selection, the labels and positions differ. CCC’s rows keep the labels 4 and 5, so ccc.loc[4, "close"] and ccc.iloc[0, 2] read the same price, and ccc.loc[0] would raise a KeyError.

The following box compares the two.

ccc = prices[prices["ticker"] == "CCC"]
print(ccc.loc[4, "close"], ccc.iloc[0, 2])
print(len(prices.loc[0:2]), len(prices.iloc[0:2]))
230.0 230.0
3 2

How do I change a selection safely?

.copy() makes a separate table, so changing it leaves the original as it was, and prices.loc[condition, column] = value writes into the original on purpose. Doubling AAA’s closes in a copy leaves prices with 101.2 and 102.0, and the .loc line marks the four busy rows in a new column.

Without .copy(), pandas 2.3 warns with a SettingWithCopyWarning. From pandas 3, a selection always behaves as a copy.

The following box changes a copy and then the original.

aaa_rows = prices[prices["ticker"] == "AAA"].copy()
aaa_rows["close"] = aaa_rows["close"] * 2
print(aaa_rows["close"].tolist(), prices.loc[prices["ticker"] == "AAA", "close"].tolist())
prices["busy"] = False
prices.loc[busy, "busy"] = True
print(prices)
[202.4, 204.0] [101.2, 102.0]
         date ticker  close  volume   busy
0  2026-03-02    AAA  101.2   12000  False
1  2026-03-03    AAA  102.0   18500   True
2  2026-03-02    BBB   55.1    8000  False
3  2026-03-03    BBB   54.0   21000   True
4  2026-03-02    CCC  230.0   15100   True
5  2026-03-03    CCC  228.4   16000   True

Your turn

Print the volumes of the rows where the ticker is AAA or BBB and more than 15,000 shares traded. You should see [18500, 21000].

.isin() tests the tickers and & adds the volume test.

print(prices.loc[prices["ticker"].isin(["AAA", "BBB"]) & busy, "volume"].tolist())
[18500, 21000]