Selecting and Filtering
Select Rows by Position (iloc)
Pick rows by integer position from a list of records — first collecting target
records via a position list, then extracting a field. With pandas, df.iloc
accepts a slice or a list of integers and returns a sub-DataFrame in one step.
By hand
Loop over a positions list, appending records[i] to selected. Then loop
over selected to collect the target field.
naive.py
Replay: real traced execution (multi-file project)
# trace: ignore records
records = [
{'name': 'al', 'age': 20},
{'name': 'bo', 'age': 25},
{'name': 'cy', 'age': 22},
{'name': 'di', 'age': 28},
]
positions = [0, 2]
selected = []
for i in positions:
selected.append(records[i])
names = []
for r in selected:
names.append(r['name'])
print('RESULT:', names)
{'name': 'al', 'age': 20},
2records = [3 {'name': 'al', 'age': 20},4 {'name': 'bo', 'age': 25},{'name': 'bo', 'age': 25},
3{'name': 'al', 'age': 20},4{'name': 'bo', 'age': 25},5{'name': 'cy', 'age': 22},{'name': 'cy', 'age': 22},
4{'name': 'bo', 'age': 25},5{'name': 'cy', 'age': 22},6{'name': 'di', 'age': 28},{'name': 'di', 'age': 28},
5 {'name': 'cy', 'age': 22},6 {'name': 'di', 'age': 28},7]records = [
1# trace: ignore records2records = [3 {'name': 'al', 'age': 20},positions ← [0, 2]
7]8positions = [0, 2]9selected = []values this step[0, 2]positionsselected ← []
8positions = [0, 2]9selected = []10for i in positions:values this step[]selectedi ← 0
9selected = []10for i in positions:11 selected.append(records[i])values this step0iselected ← [{'name': 'al', 'age': 20}]
10for i in positions:11 selected.append(records[i])12names = []values this step[] → [{'name': 'al', 'age': 20}]selectedi ← 2
9selected = []10for i in positions:11 selected.append(records[i])values this step0 → 2iselected ← [{'name': 'al', 'age': 20}, {'name': 'cy', 'age': 22}]
10for i in positions:11 selected.append(records[i])12names = []values this step[{'name': 'al', 'age': 20}] → [{'name': 'al', 'age': 20}, {'name': 'cy', 'age': 22}]selectedfor i in positions:
9selected = []10for i in positions:11 selected.append(records[i])names ← []
11 selected.append(records[i])12names = []13for r in selected:values this step[]namesr ← {'name': 'al', 'age': 20}
12names = []13for r in selected:14 names.append(r['name'])values this step{'name': 'al', 'age': 20}rnames ← ['al']
13for r in selected:14 names.append(r['name'])15print('RESULT:', names)values this step[] → ['al']namesr ← {'name': 'cy', 'age': 22}
12names = []13for r in selected:14 names.append(r['name'])values this step{'name': 'al', 'age': 20} → {'name': 'cy', 'age': 22}rnames ← ['al', 'cy']
13for r in selected:14 names.append(r['name'])15print('RESULT:', names)values this step['al'] → ['al', 'cy']namesfor r in selected:
12names = []13for r in selected:14 names.append(r['name'])stdout ← RESULT: ['al', 'cy']
14 names.append(r['name'])15print('RESULT:', names)values this stepRESULT: ['al', 'cy']stdout
With pandas
df.iloc[0:2] selects a contiguous slice by position (end-exclusive).
df.iloc[[0, 2]] selects an arbitrary list of positions. Both return a
sub-DataFrame preserving the original column structure.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
records = [
{'name': 'al', 'age': 20},
{'name': 'bo', 'age': 25},
{'name': 'cy', 'age': 22},
{'name': 'di', 'age': 28},
]
df = pd.DataFrame(records)
slice2 = df.iloc[0:2]
by_pos = df.iloc[[0, 2]]
print('slice [0:2] names:', slice2['name'].tolist())
print('index [0,2] names:', by_pos['name'].tolist())
print('index [0,2] shape:', by_pos.shape)
print('RESULT:', by_pos['name'].tolist())
slice [0:2] names: ['al', 'bo']
index [0,2] names: ['al', 'cy']
index [0,2] shape: (2, 2)
RESULT: ['al', 'cy']
Implementation notes
ilocis positional: it counts rows from 0 regardless of the index label. Slices are end-exclusive (iloc[0:2]→ rows 0 and 1), exactly like Python list slicing.- Passing a list (
iloc[[0, 2]]) selects non-contiguous rows; passing a slice (iloc[0:2]) selects a contiguous range. The return type is a DataFrame in both cases. ilocalso accepts a second argument for column positions:df.iloc[0:2, 0]returns the first column of the first two rows as a Series.- Contrast with
loc(seeselect-rows-loc):locuses index labels, not integer positions, and its slices are end-inclusive.