Skip to content

Pandas: Series

Introduction

Pandas Series is one of the foundational data structures provided by the Pandas library. In this tutorial, we'll deep-dive into the concept, covering its creation, manipulation, and various operations you can perform on it. This guide will enable both beginners and advanced users to harness the full potential of the Pandas Series.

What is a Pandas Series?

A Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floats, Python objects, etc.). It comes with both a data sequence and an associated label sequence called its index.

Creating a Pandas Series

From a List

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])
print(s)

From a Dictionary

data = {'a': 1, 'b': 2, 'c': 3}
s = pd.Series(data)
print(s)

From a Scalar

If data is a scalar value, an index must be provided:

s = pd.Series(5, index=['a', 'b', 'c'])
print(s)

Accessing Data in a Series

You can access individual elements of a Series similar to arrays and dictionaries:

Using the Position

print(s[0])

Using the Label

print(s['a'])

Basic Operations

Arithmetic Operations

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
print(s1 + s2)

Boolean Operations

print(s1 > 2)

Handling Missing Data

Pandas Series provides methods to handle missing data effectively:

s = pd.Series([1, 2, 3, None])
print(s.isnull())

Useful Methods

describe():

Gives a quick statistic summary of your data.

print(s.describe())

value_counts():

Counts unique values.

s = pd.Series(['apple', 'orange', 'apple', 'banana'])
print(s.value_counts())

Series with Datetime

Pandas Series can also handle datetime objects, making time series analysis a breeze:

dates = pd.date_range('20230101', periods=6)
s = pd.Series(dates)
print(s)

Conclusion

Pandas Series provides a robust set of functionalities to handle and manipulate one-dimensional data in Python. This tutorial covered its basics, but there's always more to explore. Make sure to consult the official Pandas documentation for a comprehensive list of functionalities.


Version 1.0

This is currently an early version of the learning material and it will be updated over time with more detailed information.

A video will be provided with the learning material as well.

Be sure to subscribe to stay up-to-date with the latest updates.

Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.