Complete worked example
python
import pandas as pd
def inspect_example(rows):
if any(set(row) != {"role_id", "score"} for row in rows):
raise ValueError("expected role_id and score fields")
frame = pd.DataFrame(rows, columns=["role_id", "score"])
return frame.astype({"role_id": "string", "score": "Float64"})- Reasoning
- The worked function accepts a list of records with exactly role_id and score. It rejects missing or extra fields before constructing the table, then casts each column explicitly. Empty input still produces both named columns with their declared dtypes. A missing score remains missing in Float64; it is not silently converted into an observed zero. The returned object is a real DataFrame, rather than a list formatted to resemble one. Trace two records and inspect columns, shape and dtypes independently. Selecting one named column yields a Series. Selecting a list of column names yields a DataFrame, even when that list contains only one name.