Series

A Series is a one-dimensional labelled array — a single column of data.

Syntaxpd.Series(data, index=labels)

A Series is a 1D array with an index (labels). Think of it as one column of a spreadsheet.

Index power

Because each value has a label, you can look items up by name, not just position. Vectorized math works just like NumPy.

Example

Example · python
import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

print(s['b'])       # 20  (label lookup)
print(s * 2)        # each value doubled
print(s.mean())     # 20.0
# a    10
# b    20
# c    30
# dtype: int64

When to use it

  • A data analyst stores a column of customer ages as a pandas Series to compute the mean, median, and value counts in one line each.
  • An ML engineer extracts the target label column from a DataFrame as a Series to pass directly to scikit-learn's fit method.
  • A financial analyst creates a Series with a DatetimeIndex to represent monthly revenue, enabling time-based slicing and resampling.

More examples

Create a Series from a list

Creates a named Series from a Python list and computes its mean, showing the basic structure of pandas' 1D labelled array.

Example · python
import pandas as pd

ages = pd.Series([25, 32, 47, 19, 38], name='age')
print(ages)
print(ages.mean())

Series with a custom index

Assigns meaningful string labels as the index, then accesses a single quarter and filters for quarters above a threshold using boolean indexing.

Example · python
import pandas as pd

revenue = pd.Series(
    [12000, 15400, 9800, 18200],
    index=['Q1', 'Q2', 'Q3', 'Q4'],
    name='revenue'
)
print(revenue['Q3'])
print(revenue[revenue > 12000])

Series operations and value counts

Counts occurrences of each category with value_counts and lists unique labels, two essential steps in exploratory analysis of a label column.

Example · python
import pandas as pd

categories = pd.Series(['cat', 'dog', 'cat', 'bird', 'dog', 'cat'])
print(categories.value_counts())
print(categories.unique())

Discussion

  • Be the first to comment on this lesson.