← Data analysis with Python

Rows and columns

See a dataset as a table: one row per thing, one column per fact.

Start this lesson

Almost every dataset is a table. Each row is one thing you observed: one sale, one day, one student. Each column is one fact recorded for every row: the price, the temperature, the score. Before doing any math, ask "what is one row?" because every later step (counting, averaging, grouping) depends on it. In plain Python, a small table fits nicely in a list of dictionaries: the list holds the rows, and each dictionary maps column names to values.

students = [
    {"name": "Amara", "grade": 10, "score": 88},
    {"name": "Luis", "grade": 11, "score": 92},
    {"name": "Mei", "grade": 10, "score": 79},
]
print(len(students), "rows")
print(list(students[0].keys()))
print(students[1]["score"])

len() counts the rows (3). The keys of any row are the column names. students[1]["score"] reads one cell: row 1 (Luis, since counting starts at 0), column "score".

A column should hold the same kind of fact in every row. If one row has "score": 88 and another has "score": "absent", later math will break, so it is worth noticing early.

Rows are often called records or observations. Columns are often called fields, variables or features. They all mean the same thing here.