Boolesche Indexierung auf DataFrames
Filtern Sie Zeilen, indem Sie eine boolesche Bedingung auf eine Spalte anwenden und mehrere Bedingungen mit den Operatoren & und | kombinieren.
Boolesche Indexierung auf DataFrames ist eine kostenlose Pandas & NumPy Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Pandas & NumPy Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
What Is Boolean Indexing?
Boolean indexing lets you filter rows in a DataFrame by applying a condition that produces a Series of True and False values. Only rows where the condition is True are returned. This is one of the most frequently used selection techniques in Pandas because it is both readable and fast.
For example, given a sales DataFrame, you can instantly retrieve all rows where revenue exceeded 1000 without writing a loop.
import pandas as pd
df = pd.DataFrame({
'product': ['A', 'B', 'C', 'D'],
'revenue': [500, 1500, 800, 2000]
})
# Boolean condition produces a Series of True/False
mask = df['revenue'] > 1000
print(mask)
# 0 False
# 1 True
# 2 False
# 3 TrueApplying the Boolean Mask
Once you have a boolean mask, you pass it inside square brackets on the DataFrame to select only the matching rows. The result is a new DataFrame — the original is never modified. The row indices from the original are preserved, so you always know where each row came from.
This pattern — create mask, then apply — is the standard Pandas idiom for row filtering.
import pandas as pd
df = pd.DataFrame({
'product': ['A', 'B', 'C', 'D'],
'revenue': [500, 1500, 800, 2000]
})
mask = df['revenue'] > 1000
filtered = df[mask]
print(filtered)
# product revenue
# 1 B 1500
# 3 D 2000Inline Conditions Without a Mask Variable
You do not need to assign the boolean Series to a variable. You can write the condition inline directly inside the brackets: df[df['col'] > value]. This one-liner style is very common in data analysis notebooks because it keeps code compact and readable.
Both approaches — mask variable and inline — produce identical results. Use a variable when the condition is complex or reused; use inline for simple one-off filters.
import pandas as pd
df = pd.DataFrame({
'city': ['NYC', 'LA', 'Chicago', 'Houston'],
'population': [8_336_817, 3_979_576, 2_693_976, 2_320_268]
})
# Inline boolean filter
large_cities = df[df['population'] > 3_000_000]
print(large_cities)
# city population
# 0 NYC 8336817
# 1 LA 3979576Combining Conditions with & (AND)
To require that both conditions are true, combine them with the & operator — not Python's and keyword, which does not work element-wise on arrays. Each condition must be wrapped in parentheses because & has higher operator precedence than comparison operators.
The result keeps only rows where every sub-condition evaluates to True.
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Carol', 'Dave'],
'age': [25, 35, 28, 45],
'salary': [50000, 90000, 70000, 120000]
})
# Both conditions must be true
result = df[(df['age'] > 27) & (df['salary'] > 60000)]
print(result)
# name age salary
# 1 Bob 35 90000
# 2 Carol 28 70000Combining Conditions with | (OR)
Use the | operator to keep rows where at least one condition is true. As with &, each sub-condition must be in parentheses. The | operator performs element-wise logical OR across the boolean arrays.
Mixing & and | is perfectly valid — just be careful with parentheses to enforce the correct evaluation order and avoid subtle logic bugs.
import pandas as pd
df = pd.DataFrame({
'product': ['Laptop', 'Mouse', 'Monitor', 'Keyboard'],
'price': [1200, 25, 300, 80],
'category': ['Electronics', 'Accessories', 'Electronics', 'Accessories']
})
# Rows where price > 200 OR category is Accessories
result = df[(df['price'] > 200) | (df['category'] == 'Accessories')]
print(result)
# product price category
# 0 Laptop 1200 Electronics
# 1 Mouse 25 Accessories
# 2 Monitor 300 Electronics
# 3 Keyboard 80 AccessoriesNegating a Condition with ~
The tilde ~ operator inverts a boolean Series — it turns True into False and vice versa. Use ~ to express 'NOT' logic: keep rows that do not match a condition. This is cleaner than constructing the opposite condition manually.
For example, df[~df['status'].isin(['cancelled', 'refunded'])] keeps all rows except those two statuses.
import pandas as pd
df = pd.DataFrame({
'order_id': [1, 2, 3, 4],
'status': ['shipped', 'cancelled', 'delivered', 'refunded']
})
# Keep only rows that are NOT cancelled
active = df[~(df['status'] == 'cancelled')]
print(active)
# order_id status
# 0 1 shipped
# 2 3 delivered
# 3 4 refundedFiltering on String Values
Boolean indexing works just as well on text columns. You can filter for an exact match with ==, or use .str methods like .str.startswith(), .str.contains(), or .str.upper() inside the condition for flexible text filtering.
String comparisons in Pandas are case-sensitive by default, so 'NYC' and 'nyc' are treated as different values. Normalise case first if needed.
import pandas as pd
df = pd.DataFrame({
'country': ['USA', 'Germany', 'UK', 'USA', 'France'],
'sales': [400, 200, 150, 600, 300]
})
# Filter rows where country equals USA
us_sales = df[df['country'] == 'USA']
print(us_sales)
# country sales
# 0 USA 400
# 3 USA 600
# Filter rows where country starts with 'G'
g_countries = df[df['country'].str.startswith('G')]
print(g_countries)
# country sales
# 1 Germany 200Filtering on Multiple Columns
Complex analyses often require conditions across multiple columns combined with & and |. Break very long conditions into named boolean Series for readability — each acting as a named sub-filter that you combine at the end.
This approach makes your intent clear: each line reads like an English sentence describing one part of the filter logic.
import pandas as pd
df = pd.DataFrame({
'region': ['North', 'South', 'North', 'East'],
'year': [2022, 2023, 2023, 2022],
'profit': [5000, -200, 8000, 3000]
})
is_north = df['region'] == 'North'
is_2023 = df['year'] == 2023
is_profitable = df['profit'] > 0
result = df[is_north & is_2023 & is_profitable]
print(result)
# region year profit
# 2 North 2023 8000Using np.where for Conditional Columns
Boolean indexing filters rows, but sometimes you want to add a new column based on a condition rather than drop rows. np.where(condition, value_if_true, value_if_false) is the vectorised equivalent of an if/else applied element-wise across a column, and it is much faster than using apply with a lambda.
import pandas as pd
import numpy as np
df = pd.DataFrame({
'score': [45, 72, 88, 61, 95]
})
# Create a new column based on a condition
df['grade'] = np.where(df['score'] >= 70, 'Pass', 'Fail')
print(df)
# score grade
# 0 45 Fail
# 1 72 Pass
# 2 88 Pass
# 3 61 Fail
# 4 95 PassAssigning Values to a Filtered Subset
You can update values in place for filtered rows using .loc[mask, column]. This is the safe way to set values on a subset — using chained indexing like df[mask]['col'] = value may raise a SettingWithCopyWarning because it operates on a copy.
Always prefer df.loc[mask, 'col'] = value when you want to modify the original DataFrame in place.
import pandas as pd
df = pd.DataFrame({
'item': ['A', 'B', 'C', 'D'],
'stock': [0, 5, 0, 12]
})
# Mark out-of-stock items
df.loc[df['stock'] == 0, 'status'] = 'Out of Stock'
df.loc[df['stock'] > 0, 'status'] = 'Available'
print(df)
# item stock status
# 0 A 0 Out of Stock
# 1 B 5 Available
# 2 C 0 Out of Stock
# 3 D 12 AvailableCounting and Summing the Mask
Since boolean values are 1 (True) and 0 (False) under the hood, you can call .sum() on a mask to count matching rows, or .mean() to get the fraction of rows that match. These quick counts help you verify your filter logic before applying it to select rows.
import pandas as pd
df = pd.DataFrame({'value': [10, 55, 30, 80, 20, 70]})
mask = df['value'] > 40
print('Count of rows > 40:', mask.sum()) # 3
print('Fraction > 40:', mask.mean().round(2)) # 0.5
# Now apply
print(df[mask])
# value
# 1 55
# 3 80
# 5 70Quick Check
Test your understanding of boolean indexing on DataFrames.
Lesson Recap
In this lesson you learned: boolean masks filter rows by applying a condition to a column, & and | operators combine multiple conditions (not Python's and/or), and ~ inverts a boolean mask for NOT logic. Use df.loc[mask, col] = value to safely assign values to filtered rows. Next up we explore the query() method for writing filters as readable strings.
Häufig gestellte Fragen
Ist die Lektion „Boolesche Indexierung auf DataFrames“ kostenlos?
Ja — der vollständige Text von „Boolesche Indexierung auf DataFrames“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Pandas & NumPy Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Boolesche Indexierung auf DataFrames“?
Filtern Sie Zeilen, indem Sie eine boolesche Bedingung auf eine Spalte anwenden und mehrere Bedingungen mit den Operatoren & und | kombinieren. Du übst Pandas & NumPy Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Pandas & NumPy Academy zu starten?
Keine Vorkenntnisse erforderlich. Pandas & NumPy Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Boolesche Indexierung auf DataFrames“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Pandas & NumPy Academy-Lektion Code schreiben und ausführen?
Ja. Jede Pandas & NumPy Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Boolesche Indexierung auf DataFrames
- Die Methode query()
- Filter mit isin() und between()
- Spalten nach Muster auswählen