Filtering data with conditions
Conditional selection means filtering the rows you want using a rule. Just like using WHERE in SQL, in Pandas you use the pattern df[df["Column"] condition].
python
import pandas as pd
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie", "Daisy"],
"Age": [25, 30, 35, 22],
"Score": [82, 91, 76, 95]
})
high_scores = df[df["Score"] >= 90]
adults = df[df["Age"] >= 30]
print("High scores:")
print(high_scores)
print("
Age 30 and above:")
print(adults)df["Score"] >= 90 produces a list of True/False values. Pandas then picks out only the rows where it's True and returns a new DataFrame. That's why this is so commonly used for generating reports, segmenting customers, and checking sales thresholds.
You should see
High scores: Name Age Score 1 Bob 30 91 3 Daisy 22 95 Age 30 and above: Name Age Score 1 Bob 30 91 2 Charlie 35 76Info
If you want to use more than one condition, & and | and wrap each condition in parentheses. For example, df[(df["Age"] >= 30) & (df["Score"] >= 80)]