Complete worked example
python
import pandas as pd
def inspect_example(rows, threshold):
frame = pd.DataFrame(rows, columns=["id", "score"]).astype({"id": "string", "score": "Float64"})
eligible = frame["score"].ge(threshold).fillna(False)
selected = frame.loc[eligible, ["id", "score"]].reset_index(drop=True)
return selected.iloc[:2].reset_index(drop=True)- Reasoning
- Build a typed two-column frame, form a boolean mask from score and use loc with both that mask and an explicit list of column names. Reset the retained row index for the presentation table. The final iloc slice chooses the first two qualifying rows without relying on whatever original labels survived filtering. Missing scores do not satisfy the threshold, because fillna(False) makes that policy explicit. For scores nine, two, eight and seven at a threshold of seven, eligibility is decided before the limit. The first two qualifying roles are the first and third original rows. Limiting first would lose a valid candidate.