The essentials
A Series is a one-dimensional labelled array. It has values and an index (labels). If no index is given, the default index is 0, 1, 2 and so on.
Key points
- pd.Series([10, 20, 30]) creates a Series with index 0, 1, 2.
- pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']) uses custom labels.
- pd.Series(5, index=[1, 2, 3]) repeats the scalar 5 for each label.
- pd.Series({'Jan': 31, 'Feb': 28}) uses dictionary keys as the index.
- A NumPy array can also be passed to pd.Series().
- Accessing: s['b'] gives the value with label b; s.iloc[1] gives the value at position 1; s.loc['b'] also uses the label.
- Slicing by position excludes the end: s.iloc[0:2] has 2 elements. Slicing by label includes the end: s['a':'c'] has 3 elements.
- Attributes: index, values, size, dtype, name, empty.
- Methods: head(n) gives the first n elements (default 5); tail(n) gives the last n.
- Maths: operations are element-wise. s + 5 adds 5 to each value. When two Series are added, values are matched by index label; labels present in only one Series give NaN.
- Filtering: s[s > 15] returns elements greater than 15.
Exam tips
- Count carefully whether a slice is by label (end included) or by position (end excluded).
- In Series addition, look for labels that are missing in one Series; they produce NaN.
Common mistakes
- Expecting Series addition to match by position, as with lists.
- Forgetting that the default index starts at 0.
Practice questions
Log in to answer these questions and see the explanations.Create free accountLog in