Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a series in Python

Understanding Series in Python

When you're just starting with programming, it's essential to grasp the basic building blocks that you'll use to store and manipulate data. In Python, one of these fundamental structures is called a 'Series.' But what exactly is a Series?

What is a Series?

Imagine you have a shopping list. This list has items placed in a specific order: first, you have 'milk,' then 'bread,' followed by 'eggs,' and so on. In Python, this ordered list can be represented as a Series. A Series is like a column in a spreadsheet or a list with superpowers. It's a one-dimensional array (think of it as a train of data, where each data point is a carriage) that can hold any type of data—numbers, strings, and even Python objects.

How to Create a Series in Python

To work with Series in Python, we use a library called pandas. pandas is a popular open-source library that provides high-performance, easy-to-use data structures, and data analysis tools. Before you can create a Series, you need to import this library.

import pandas as pd

Now, let's create our first Series. We'll start by making a simple list of items, and then we'll convert that list into a Series.

# A simple Python list
shopping_list = ['milk', 'bread', 'eggs', 'apples']

# Converting the list to a Series
shopping_series = pd.Series(shopping_list)

When you run this code, you'll see that shopping_series prints out the items with an index (the default index is a sequence of integers starting from 0).

Indexing in Series

Just like in a real-life shopping list where you can say "the second item on the list," in a Series, you can access items using their index. In Python, the index is like a label for each item in the Series. You can use these labels to quickly find or manipulate data.

# Access the first item in the Series
first_item = shopping_series[0]
print(first_item)  # Output: 'milk'

Custom Indexes

What if you want to customize the index? Instead of numbers, you could use days of the week to plan your shopping. Here's how you can do that:

# Custom index for the Series
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']

# Create a Series with a custom index
shopping_series = pd.Series(shopping_list, index=days)

Now, instead of using numbers, you can access your shopping items by the day of the week.

Data Types in Series

Series can hold different types of data. You could have a Series of numbers, a Series of strings, or even a Series of Python objects. pandas will automatically choose the best data type to represent your data, but you can also specify it explicitly.

# Series of numbers
numbers_series = pd.Series([10, 20, 30, 40])

# Series of strings specified explicitly
strings_series = pd.Series(['red', 'green', 'blue'], dtype='str')

Operations on Series

One of the superpowers of Series is that you can perform operations on them very efficiently. For instance, if you have a Series of numbers, you can easily calculate the sum, average, or other statistical measures.

# Summing all the numbers in the Series
total = numbers_series.sum()
print(total)  # Output: 100

# Calculating the average
average = numbers_series.mean()
print(average)  # Output: 25

Handling Missing Data

Sometimes, data is incomplete or missing. In a Series, missing data is represented as NaN (Not a Number). pandas provides tools to handle missing data gracefully.

# Series with missing data
grades_series = pd.Series([90, 80, None, 70])

# Filling missing data with a value
filled_grades = grades_series.fillna(0)

Intuition and Analogies

To help you understand the concept of a Series, think of it as a train. Each carriage of the train is an element in the Series, and the name of each carriage is the index. Just like a train follows a track, a Series has a defined order that you can rely on.

When you perform operations on a Series, it's like giving instructions to each carriage. If you say "increase speed by 10," each carriage individually increases its speed.

Handling missing data in a Series is like dealing with an empty carriage. You can decide to ignore it, fill it with something, or remove it from the train altogether.


In our journey through the world of Python programming, we've explored the concept of a Series—a versatile and powerful tool for handling ordered data. Just like a well-organized shopping list can make your trip to the grocery store more efficient, mastering the use of Series in Python can enhance your data manipulation capabilities.

Whether you're summing up expenses, sorting through colors, or planning your weekly meals, think of a Series as your trusty digital notepad, neatly organizing and processing your data with ease. As you continue your programming adventure, remember that each new concept is a stepping stone to greater mastery, and Series is one of those foundational stones that pave the way to a world of data-driven possibilities. So, keep experimenting, keep learning, and let the power of Series in Python streamline your coding endeavors.