0Pricing
Pandas & NumPy Academy · Ders

isin() ve between() Filtreleri

isin() ile bir sütun değerinin listedeki değerlerden biri olduğu veya between() ile sayısal bir aralıkta bulunduğu satırları seçin.

isin() ve between() Filtreleri, CoddyKit'te ücretsiz bir Pandas & NumPy Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Pandas & NumPy Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Pandas & NumPy Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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    600

isin() 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   12

Negating 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     500

isin() 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      400

The 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   45

between() 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     50

between() 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     30

Combining 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      300

isin() 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     Infra

Performance 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    150

Quick 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.

Sıkça Sorulan Sorular

“isin() ve between() Filtreleri” dersi ücretsiz mi?

Evet — “isin() ve between() Filtreleri” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Pandas & NumPy Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Pandas & NumPy Academy kursu toplamda 4 dersten oluşur.

“isin() ve between() Filtreleri” dersinde ne öğreneceğim?

isin() ile bir sütun değerinin listedeki değerlerden biri olduğu veya between() ile sayısal bir aralıkta bulunduğu satırları seçin. Pandas & NumPy Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Pandas & NumPy Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Pandas & NumPy Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“isin() ve between() Filtreleri” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Pandas & NumPy Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Pandas & NumPy Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. DataFrames Üzerinde Boole Dizinleme
  2. query() Yöntemi
  3. isin() ve between() Filtreleri
  4. Desene Göre Sütun Seçme
← Pandas & NumPy Academy Sayfasına Dön