Series Basics
Series with Label Index
Map string labels to values with a dict loop, then look up by label. A Series
with a named index works the same way but also preserves order, supports
arithmetic alignment, and integrates with DataFrames. The replay shows data
filling label by label before lookup isolates a single entry.
By hand
With pandas
pd.Series(values, index=labels) assigns the string labels as the index.
s['b'] retrieves the value by label — the same dict-like lookup, but on a
typed, ordered structure.
naive.py
labels = ['a', 'b', 'c', 'd']
values = [10, 20, 30, 40]
data = {}
for i in range(len(labels)):
data[labels[i]] = values[i]
lookup = data['b']
print('RESULT:', (lookup, values))
library.py
import pandas as pd
from dalib.display import set_display
set_display()
labels = ['a', 'b', 'c', 'd']
values = [10, 20, 30, 40]
s = pd.Series(values, index=labels)
lookup = int(s['b'])
print('index:', s.index.tolist())
print('values:', s.tolist())
print('dtype:', s.dtype)
print('s[b]:', lookup)
print('RESULT:', (lookup, s.tolist()))
index: ['a', 'b', 'c', 'd']
values: [10, 20, 30, 40]
dtype: int64
s[b]: 20
RESULT: (20, [10, 20, 30, 40])
Implementation notes
- A Series goes beyond a plain dict in four ways: values are typed and
homogeneous (single
dtype); the index is an explicitIndexobject that participates in alignment; both label-based (s['b'],s.loc['b']) and positional access (s.iloc[1]) are available; and arithmetic between two Series aligns on index labels automatically (seeseries-arithmetic-align). int(s['b'])converts the returned numpy scalar to a plain Python int for a stable RESULT, avoiding repr differences across numpy versions.- When two Series share the same index, pandas aligns them by label during
arithmetic:
s1 + s2adds elements with matching labels, filling mismatches with NaN. - Index and values are shown via
.tolist()here because rawSeries.__repr__output varies with pandas display options.