← Data analysis with Python

Kinds of data

Numbers, categories, dates and text, and why the kind matters.

Start this lesson

Columns come in a few kinds. Numeric columns measure or count something (price, minutes, temperature), so math like averages makes sense. Categorical columns put rows into groups (store, color, member yes/no), so counting each group makes sense instead. Dates and times record when something happened. Free text holds things like comments. Knowing the kind tells you which questions make sense: the average of zip codes means nothing, even though zip codes are made of digits.

row = {"store": "North", "units": 30, "price": 3.5, "zip": "60614"}
for key, value in row.items():
    print(key, type(value).__name__)

price_text = "3.50"
print(float(price_text) * 2)

type() shows how Python stores each value. The zip is stored as text on purpose: it is a label, not an amount. Numbers that arrive as text, like "3.50", must be converted with float() or int() before doing math.

A very common surprise: data read from files arrives as text. "12" + "5" is "125", not 17. Converting types is often the first step of an analysis.