Data Science · Level 2 DataFrames

pandas DataFrames

Build a DataFrame from a list of records like an API returns, keep only the columns you need and check its shape.

  • pd.DataFrame(list of dicts)
  • columns=[...] picks and orders columns
  • df.shape is (rows, columns)

pd.DataFrame(records) turns a list of dicts, typical API JSON, into a table with one row per dict. df.shape gives (rows, columns). Trap: a key missing from a record becomes NaN, not an error.

orders = [{"id": 1, "total": 30}, {"id": 2}]
df = pd.DataFrame(orders)
df.shape              # (2, 2)
df["total"].tolist()  # [30.0, nan]

Write orders_table(orders) that returns a DataFrame with only the columns id and total, in that order, for example orders_table([{"total": 9, "id": 1, "note": "gift"}]) has the columns ['id', 'total'].

solution.py
Test Suites Pending
Output