In the previous part we installed pandas, or, if you have the Anaconda distribution of Python, we just made it clear that we already have it. In this part we’ll be talking about the Series class, which is one of the fundamental data types in pandas.
As pandas is built on numpy, we’ll import these two modules at the beginning of each part from now on:
import numpy as np
import pandas as pd
So, what is a pandas Series? It’s a one-dimensional array of indexed data. Let’s create one so that you can see what indexed data means. You can create a Series object in a couple of ways, like from a list or numpy array. Let’s start with a list:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79])
nums
As you can see, the Series object we just created contains not only the elements from the list, but also their indices. That’s what indexed data means. Besides, we can see the dtype of our elements, which in this case are floats.
Now, we can access just the values or just the indices if we need to. To do it, we will use the values and index attributes respectively:
# values
nums.values
# indices
nums.index
The values are returned as a one-dimensional numpy array, and the indices as a pandas Index object, which we are going to discuss in more detail in one of the articles in this series in near future:
type(nums.values), type(nums.index)
OPERATIONS ON SERIES OBJECTS
Accessing particular elements of a pandas Series object is easy. You just use square brackets:
# access element at index 2
nums[2]
Slicing is pretty straightforward too:
# slice from index 1 to index 4 (the latter not being included)
nums[1:4]
These are just the basics of Series in pandas. In the next part of this pandas series we’ll see how we can create Series objects. You already know you can use a list or numpy array to this end, but that’s not all.
Here’s the video version of this article: