pyodide: loading…

[practice]Pandas Fundamentals

Filtering Rows

# theory

boolean indexing

The most powerful way to filter data. Create a True/False mask and use it to select rows:

# Create boolean mask
mask = df["age"] > 25
print(mask)  # Series of True/False

# Apply mask to filter
older_people = df[mask]
# Or in one line:
older_people = df[df["age"] > 25]

operators

df[df["price"] > 50]       # Greater than
df[df["price"] >= 50]      # Greater than or equal
df[df["price"] == 50]      # Equal
df[df["price"] != 50]      # Not equal
df[df["name"] == "Alice"]  # String equality

combining conditions

Use & (and) and | (or). Wrap each condition in parentheses!

# AND - both must be true
df[(df["age"] > 25) & (df["city"] == "NYC")]

# OR - either can be true
df[(df["age"] < 20) | (df["age"] > 60)]

useful methods

# isin() - check membership in a list
df[df["city"].isin(["NYC", "LA", "Chicago"])]

# between() - range check (inclusive)
df[df["age"].between(25, 35)]

# String methods
df[df["name"].str.startswith("A")]
df[df["name"].str.contains("ali", case=False)]

# examples [3]

# example 01 · basic filtering

Filter rows based on a condition

1
2
3
4
5
6
7
8
9
🐍
Loading PythonSetting up pandas & numpy...
# example 02 · multiple conditions

Combine conditions with & (and) and | (or)

1
2
3
4
5
6
7
8
9
🐍
Loading PythonSetting up pandas & numpy...
# example 03 · using isin and between

Convenient methods for common patterns

1
2
3
4
5
6
7
8
9
10
🐍
Loading PythonSetting up pandas & numpy...

# challenges [2]

# challenge 01/02todo
Filter sales to show only products with price > 20. Print the result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
🐍
Loading PythonSetting up pandas & numpy...
# challenge 02/02todo
Filter students who have grade 'A' OR grade 'B'. Print the result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
🐍
Loading PythonSetting up pandas & numpy...

# project

# project-challenge

thread: SF Permits Analysis · reward: 50 xp

# brief

The city wants to see only permits that are currently active. Filter to show permits that are either 'issued' or 'complete' status.

# task

Filter Active Permits

# your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
🐍
Loading PythonSetting up pandas & numpy...