Build a boolean mask by testing each record's score field in a loop, then keep only the rows where the mask is True. With pandas, a single comparison on a column produces a boolean Series, and indexing a DataFrame with it returns the matching rows in one step.

By hand

With pandas

df['score'] >= k produces a boolean Series. df[mask] keeps only the rows where the Series is True and returns a sub-DataFrame.

naive.py
# trace: ignore records
records = [
    {'name': 'al', 'score': 55},
    {'name': 'bo', 'score': 82},
    {'name': 'cy', 'score': 71},
    {'name': 'di', 'score': 90},
]
k = 70
mask = []
for r in records:
    mask.append(r['score'] >= k)
names = []
for i in range(len(records)):
    if mask[i]:
        names.append(records[i]['name'])
print('RESULT:', names)
library.py
import pandas as pd
from dalib.display import set_display
set_display()

records = [
    {'name': 'al', 'score': 55},
    {'name': 'bo', 'score': 82},
    {'name': 'cy', 'score': 71},
    {'name': 'di', 'score': 90},
]
k = 70
df = pd.DataFrame(records)
mask = df['score'] >= k
filtered = df[mask]
print('mask:', mask.tolist())
print('names:', filtered['name'].tolist())
print('shape:', filtered.shape)
print('RESULT:', filtered['name'].tolist())
mask: [False, True, True, True]
names: ['bo', 'cy', 'di']
shape: (3, 2)
RESULT: ['bo', 'cy', 'di']

Implementation notes

  • A boolean Series used as a row index is called boolean indexing. The Series must have the same index as the DataFrame; pandas aligns on labels before filtering.
  • The filtered result preserves the original integer index of matching rows (bo=1, cy=2, di=3 here). Use .reset_index(drop=True) afterward if you need a fresh 0-based index.
  • df['score'] >= k is vectorized — no Python loop over values. Pandas applies the comparison to the underlying numpy array directly (see mask-from-threshold in python-numpy ch06).
  • Cross-reference: filter-with-mask (python-numpy ch06) for the array version; filter-by-threshold (python-data-basics) for the pure-Python version.