[practice]Pandas Fundamentals
Selecting Data
# theory
selecting columns
Single column (returns Series):
df["name"]
Multiple columns (returns DataFrame):
df[["name", "age"]]
loc vs iloc
- loc: Select by label (row/column names)
- iloc: Select by integer position (0, 1, 2...)
# loc - by label
df.loc[0] # First row (if index is 0, 1, 2...)
df.loc[0, "name"] # Cell at row 0, column "name"
df.loc[:, "name"] # All rows, "name" column
df.loc[0:2, ["name", "age"]] # Rows 0-2, specific columns
# iloc - by position
df.iloc[0] # First row
df.iloc[0, 1] # Row 0, column 1
df.iloc[:3, :2] # First 3 rows, first 2 columns
df.iloc[-1] # Last row# examples [3]
# example 01 · column selection
Different ways to select columns
1
2
3
4
5
6
7
8
9
🐍
# example 02 · using loc
Select by label (row index and column name)
1
2
3
4
5
6
🐍
# example 03 · using iloc
Select by integer position
1
2
3
4
5
6
7
🐍
# challenges [2]
# challenge 01/02todo
Select only the 'product' and 'price' columns from sales DataFrame and print them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🐍
# challenge 02/02todo
Use iloc to select the first 3 rows and first 2 columns of the students DataFrame.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🐍
# project
# project-challenge
thread: SF Permits Analysis · reward: 50 xp
# brief
The status dashboard only needs three columns: Permit Number, Status, and Neighborhood. Select just these columns for the report view.
# task
Create Status Report View
# 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
🐍