Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · PANDAS · CHAPTER 6

Series

On this page

The first major data structure in Pandas is the Series. A Series is similar to a NumPy array but more powerful because it includes labels — allowing each element to be indexed not only by position but also by name.

Formally, a Series is a one-dimensional labeled array that can hold data of any type: integers, floats, strings, or even Python objects. Conceptually, it resembles a column in an Excel spreadsheet or a dictionary with ordered keys.

Let’s begin by importing Pandas and NumPy:

import numpy as np
import pandas as pd

Creating a Series

A Series can be created from a Python list, NumPy array, or dictionary. We’ll start with simple examples to illustrate each approach.

Example 1: Creating a Series from a list

labels = ['a', 'b', 'c']
my_list = [10, 20, 30]

series_from_list = pd.Series(data=my_list, index=labels)
print(series_from_list)
a    10
b    20
c    30
dtype: int64

Here, each label (‘a’, ‘b’, ‘c’) identifies an element in the Series. Unlike NumPy arrays, which rely on numeric positions, Pandas allows custom index names that make data easier to interpret — much like column headers or categories in a spreadsheet.

Example 2: Creating a Series from a dictionary

data_dict = {'a': 10, 'b': 20, 'c': 30}
series_from_dict = pd.Series(data_dict)
print(series_from_dict)
a    10
b    20
c    30
dtype: int64

Example 3: Series with different data types

A Pandas Series can hold not only numbers but also strings or even functions:

mixed_series = pd.Series(['Aland', 'Bretoria', 'Caldara'])
print(mixed_series)
0       Aland
1    Bretoria
2     Caldara
dtype: object

Here, the default integer index (0, 1, 2) is used. Such flexibility allows Pandas to handle mixed or textual data — common in humanities research, such as names of authors, places, or cultural artifacts.

Working with Series

Once a Series is created, elements can be accessed using their labels or positions.

ser = pd.Series([100, 200, 300], index=['Aland', 'Bretoria', 'Caldara'])

print(ser['Bretoria'])
print(ser.iloc[0])
200
100

Here, ‘Bretoria’ refers to the label, while 0 refers to the position. This dual access system makes Series both human-readable and computationally efficient.

Example: Comparing Two Series

Let’s create two Series representing cultural funding (in million dollars) in three countries during two different decades:

funding_1990 = pd.Series([120, 80, 90], index=['Aland', 'Bretoria', 'Caldara'])
funding_2000 = pd.Series([150, 100, 130], index=['Aland', 'Bretoria', 'Caldara'])

print("Funding in 1990:")
print(funding_1990)

print("\nFunding in 2000:")
print(funding_2000)

# Calculate increase
increase = funding_2000 - funding_1990
print("\nIncrease in funding:")
print(increase)
Funding in 1990:
Aland       120
Bretoria     80
Caldara      90
dtype: int64

Funding in 2000:
Aland       150
Bretoria    100
Caldara     130
dtype: int64

Increase in funding:
Aland       30
Bretoria    20
Caldara     40
dtype: int64

Here, Pandas automatically aligns values by their labels — even if the Series have different orders or missing elements. This label alignment makes arithmetic operations intuitive and eliminates many manual checks required in NumPy.

Teaching edition · verified experiments · PDF and EPUB editions not generated.