Pandas Series
Python · Pandas Basics
Theory
A pandas Series is a one-dimensional array of values with a label for each position, called the index. pd.Series([100, 250, 300], index=['Jan', 'Feb', 'Mar']) stores three sales figures labelled by month, so sales['Feb'] returns 250. If you do not give an index, pandas uses 0, 1, 2 and so on. Every Series also has a dtype (int64 here) and an optional name.
A Series behaves like a NumPy array with labels: sales * 2 doubles every value, sales.sum() adds them, and sales[sales > 150] filters with a boolean mask, returning Feb 250 and Mar 300 with their labels kept. When two Series are combined, pandas aligns them by index label, not position, and labels present in only one give NaN. Each column of a DataFrame is a Series.
Why we use it
Labels make data self-describing: you work with sales['Feb'] instead of remembering that February is position 1. Alignment by label prevents silent mistakes when combining data from different sources.
When to use it
Use a Series for a single column of data or a labelled list of values, such as monthly totals. When you have several related columns, use a DataFrame; for plain numeric maths with no labels, a NumPy array is lighter.
How it works
- pd.Series(values, index=labels)
- Pair each value with its index label
- Infer one dtype for the values
- Computing on values? Yes: Element-wise, aligned by label. No: Look up via sales['Feb'] or .loc.
- Labelled result Series
Syntax
pd.Series(data, index=labels, name='col')Example
import pandas as pd
sales = pd.Series([100, 250, 300], index=['Jan','Feb','Mar'])
print(sales)Key points
- The building block that DataFrame columns are made of
- Supports the same vectorised operations as NumPy arrays
- The index labels each value; default is 0, 1, 2 ...
- Operations between Series align on index labels; unmatched labels give NaN
- Use .loc for label lookups and .iloc for positions