isin() 与 between() 筛选
使用 isin() 选择列值属于某个列表的行,或使用 between() 选择列值处于某个数值范围内的行。
isin() 与 between() 筛选 是 CoddyKit 上的免费 Pandas & NumPy Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Pandas & NumPy Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Pandas & NumPy Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The isin() Method Overview
isin() checks whether each element of a Series is contained in a given list (or set) of values. It returns a boolean Series that you can use directly for row filtering. This is more concise and often faster than chaining multiple == comparisons with |.
For example, instead of (df['country'] == 'USA') | (df['country'] == 'UK'), you write df['country'].isin(['USA', 'UK']).
import pandas as pd
df = pd.DataFrame({
'country': ['USA', 'Germany', 'UK', 'France', 'USA'],
'sales': [400, 200, 150, 300, 600]
})
# Check membership
mask = df['country'].isin(['USA', 'UK'])
print(mask.tolist()) # [True, False, True, False, True]
result = df[mask]
print(result)
# country sales
# 0 USA 400
# 2 UK 150
# 4 USA 600isin() with Sets for Speed
You can pass a Python set instead of a list to isin(). Membership look-up in a set is O(1) — it doesn't get slower as the list grows. This matters when your allowed values list has thousands of entries, making set-based isin() significantly faster than list-based isin().
import pandas as pd
df = pd.DataFrame({
'sku': ['A001', 'B002', 'A003', 'C004', 'B005'],
'qty': [10, 5, 3, 8, 12]
})
# Use a set for fast membership check
allowed_skus = {'A001', 'A003', 'B005'}
result = df[df['sku'].isin(allowed_skus)]
print(result)
# sku qty
# 0 A001 10
# 2 A003 3
# 4 B005 12Negating isin() with ~
Combine isin() with the ~ operator to filter rows where a column does not contain the specified values. This is the cleanest way to exclude a list of specific values, far more readable than building a chain of != comparisons.
import pandas as pd
df = pd.DataFrame({
'status': ['active', 'cancelled', 'shipped', 'refunded', 'active'],
'amount': [100, 200, 300, 150, 500]
})
bad_statuses = ['cancelled', 'refunded']
# Keep all rows NOT in the excluded list
result = df[~df['status'].isin(bad_statuses)]
print(result)
# status amount
# 0 active 100
# 2 shipped 300
# 4 active 500isin() with a DataFrame Column as the List
A powerful trick is to pass the values from another DataFrame's column to isin(). This is equivalent to a SQL semi-join: keep rows in df1 whose key appears in df2. Unlike a full merge, this does not duplicate rows or add extra columns — it just filters.
import pandas as pd
orders = pd.DataFrame({
'order_id': [1, 2, 3, 4, 5],
'revenue': [100, 200, 300, 400, 500]
})
vip_orders = pd.DataFrame({'order_id': [2, 4]})
# Keep orders whose ID appears in vip_orders
result = orders[orders['order_id'].isin(vip_orders['order_id'])]
print(result)
# order_id revenue
# 1 2 200
# 3 4 400The between() Method Overview
between(left, right) returns a boolean Series that is True where the values fall within the closed range [left, right] (inclusive by default). It replaces two-sided conditions like (df['age'] >= 18) & (df['age'] <= 65) with a single, readable call.
The inclusive parameter controls boundary inclusion: 'both' (default), 'left', 'right', or 'neither'.
import pandas as pd
df = pd.DataFrame({
'age': [15, 22, 35, 67, 45, 8]
})
# Select working-age population
result = df[df['age'].between(18, 65)]
print(result)
# age
# 1 22
# 2 35
# 4 45between() with inclusive Parameter
By default, both endpoints are included in the range. Setting inclusive='left' excludes the right boundary (useful for half-open intervals like time bins), inclusive='right' excludes the left, and inclusive='neither' excludes both endpoints for a strict open interval.
import pandas as pd
df = pd.DataFrame({'score': [0, 50, 100, 75, 50]})
# Include only scores strictly between 50 and 100
strict = df[df['score'].between(50, 100, inclusive='neither')]
print(strict)
# score
# 3 75
# Include 50 but not 100
left_closed = df[df['score'].between(50, 100, inclusive='left')]
print(left_closed)
# score
# 1 50
# 3 75
# 4 50between() on Date Columns
between() works on datetime columns too. Pass date strings or Timestamp objects as the boundaries, and Pandas will perform the comparison on the underlying datetime values. This is a clean way to slice a time window without converting dates to integers.
import pandas as pd
df = pd.DataFrame({
'date': pd.to_datetime(['2024-01-01', '2024-06-15', '2024-11-20', '2025-03-01']),
'value': [10, 20, 30, 40]
})
# Filter rows in the year 2024
result = df[df['date'].between('2024-01-01', '2024-12-31')]
print(result)
# date value
# 0 2024-01-01 10
# 1 2024-06-15 20
# 2 2024-11-20 30Combining isin() and between()
You can combine isin() and between() in the same boolean expression using & and |. This creates compact, readable filters that would otherwise require many nested conditions. Always enclose each call in parentheses when combining.
import pandas as pd
df = pd.DataFrame({
'region': ['North', 'South', 'East', 'North', 'West'],
'revenue': [100, 500, 200, 300, 400]
})
# Rows in North or South regions AND revenue between 200 and 400
result = df[
df['region'].isin(['North', 'South']) &
df['revenue'].between(200, 400)
]
print(result)
# region revenue
# 3 North 300
# 1 South 500 <- actually excluded (500 > 400)
# 3 North 300isin() on Multiple Columns with Any
When you want to check if any of several columns contains a value from a list, you can call isin() on the entire DataFrame and use .any(axis=1) to collapse the result row-wise. This is useful for searching across multiple tag or category columns simultaneously.
import pandas as pd
df = pd.DataFrame({
'tag1': ['python', 'java', 'sql'],
'tag2': ['sql', 'python', 'go'],
'topic': ['Database', 'Backend', 'Infra']
})
target_langs = ['python', 'sql']
# Check if either tag column matches
mask = df[['tag1', 'tag2']].isin(target_langs).any(axis=1)
result = df[mask]
print(result)
# tag1 tag2 topic
# 0 python sql Database
# 1 java python Backend
# 2 sql go InfraPerformance Tip: isin() vs Multiple ==
For a small list of 2-3 values, the difference between isin() and chained == conditions is negligible. But for large lists (hundreds of values), isin() is dramatically faster because it converts the list to a hash set internally and performs O(1) look-ups per element rather than sequential comparison.
Always prefer isin() over a long chain of | conditions for cleaner code and better performance.
import pandas as pd
import numpy as np
# 1 million row DataFrame
df = pd.DataFrame({'id': np.random.randint(0, 10000, size=1_000_000)})
allowed = list(range(0, 500)) # 500 allowed IDs
# isin() is the right approach here — fast hash lookup
result = df[df['id'].isin(allowed)]
print(result.shape) # around (50000, 1)Real-World Filter: Product Catalogue
Here is a realistic example combining isin() for category filtering and between() for price range filtering — a common pattern in e-commerce analytics. The two filters together select exactly the product subset needed for a targeted promotion.
import pandas as pd
products = pd.DataFrame({
'name': ['Laptop', 'Mouse', 'Monitor', 'Keyboard', 'Webcam'],
'category': ['Computing', 'Accessories', 'Displays', 'Accessories', 'Peripherals'],
'price': [1200, 25, 300, 80, 150]
})
# Products in Accessories or Peripherals, priced 50-200
eligible = products[
products['category'].isin(['Accessories', 'Peripherals']) &
products['price'].between(50, 200)
]
print(eligible)
# name category price
# 3 Keyboard Accessories 80
# 4 Webcam Peripherals 150Quick Check
Test your understanding of isin() and between() filters.
Lesson Recap
In this lesson you learned: isin() checks set membership and is faster than chaining multiple == conditions, ~isin() excludes a list of values, and between() filters a closed range with an optional inclusive parameter. Both methods work on numeric, string, and datetime columns. Next up we explore how to select DataFrame columns matching a name pattern.
常见问题解答
「isin() 与 between() 筛选」课时是免费的吗?
是的 — 「isin() 与 between() 筛选」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Pandas & NumPy Academy 课程的其余内容,请升级到 CoddyKit PRO。 Pandas & NumPy Academy 课程共包含 4 节课。
「isin() 与 between() 筛选」这节课中我会学到什么?
使用 isin() 选择列值属于某个列表的行,或使用 between() 选择列值处于某个数值范围内的行。 你通过在浏览器中直接运行的动手代码来练习 Pandas & NumPy Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Pandas & NumPy Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Pandas & NumPy Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「isin() 与 between() 筛选」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Pandas & NumPy Academy 课中编写并运行代码吗?
能。每节 Pandas & NumPy Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- DataFrames 上的布尔索引
- query() 方法
- isin() 与 between() 筛选
- 按模式选择列