← Data analysis with Python

Series: a labeled column

One column of values with labels, plus built-in math.

Start this lesson

pandas is the most widely used Python library for tables. Its simplest object is a Series: a list of values with a label (the index) on each one. Two things make it powerful. First, it has summaries built in: .sum(), .mean(), .max(), .idxmax() and more. Second, math applies to every value at once, so steps * 2 doubles the whole column without a loop. This "whole column at once" style is called vectorized code.

import pandas as pd

steps = pd.Series([6200, 8100, 5400, 9900, 7300],
                  index=["Mon", "Tue", "Wed", "Thu", "Fri"])
print(steps.mean())
print(steps.idxmax())
print(steps / 1000)

The mean is 7380. idxmax() gives the LABEL of the largest value (Thu), while max() would give the value itself (9900). steps / 1000 divides every value at once.

Without an index argument, pandas labels the values 0, 1, 2, ...

steps["Wed"] reads one value by its label.