“Come friends, it’s not too late to seek a newer world.”
― Alfred Tennyson
Contents
1. Introduction
Pandas is a powerful toolkit providing data-analysis tools and structures for the python programming language.
Among the most important artifacts provided by pandas is the Series. In this article, we introduce the Series class from a beginner’s perspective. That means you do not need to know anything about pandas or data-analysis to understand this tutorial.
2. What is a Series?
A Series is similar to a list or an array in python. It represents a series of values (numeric or otherwise) such as a column of data. Think of it as a python list on steroids. It provides additional functionality, methods and operators which makes it a more powerful version of a list.
To get started using a Series, you need to import the pandas toolkit into your python program. Add the following line to the beginning of your program and you are good to go.
import pandas as pd
3. Create Series from List
Let us now learn how to create a Series. Since a Series is similar to a list, let us use a list to create a Series.
# Create a Series from a list ser = pd.Series([1, 3, 5, 7]) print ser # prints 0 1 1 3 2 5 3 7 dtype: int64
Notice that when the Series is printed, two columns of numbers are printed. The first column is called the index. It normally starts from 0 and runs all the way to (N-1) where N is the size of the list.
The second column is the actual data that we specified.
4. Using Dict to Initialize Series
It is also possible to use a python dictionary (called a dict) to initialize a Series. In this case, the Series takes its index from the keys of the dict as shown by this example.
prices = {'apple': 4.99, 'banana': 1.99, 'orange': 3.99, 'grapes': 0.99} ser = pd.Series(prices) print ser # prints: apple 4.99 banana 1.99 grapes 0.99 orange 3.99 dtype: float64
5. Initialize Series from Scalar
You can also use a scalar to initialize a Series. In this case, all elements of the Series is initialized to the same value. When used with a scalar for initialization, an index array can be specified. In this case, the size of the Series is the same as the size of the index array.
In the following example, we use the range() function to specify the index (and thus the size of the Series).
ser = pd.Series(2, index=range(0, 5)) print ser # prints 0 2 1 2 2 2 3 2 4 2 dtype: int64
6. Some Other Ways of Creating a Series
Here are some additional ways of initializing a Series.
6.1. A Series of Odd Numbers
We use the range() function to create a Series of odd numbers.
print pd.Series(range(1, 10, 2)) # prints 0 1 1 3 2 5 3 7 4 9 dtype: int64
6.2. With an Alphabetic Index
print pd.Series(range(1, 15, 3), index=[x for x in 'abcde']) # prints a 1 b 4 c 7 d 10 e 13 dtype: int64
6.3. A Series with Random Numbers
Use a random range to initialize a Series:
print pd.Series(random.sample(xrange(100), 6)) # prints 0 61 1 81 2 11 3 78 4 29 5 92 dtype: int64
6.4. Combining Lists
If you have the data in one list and the index in another list, there are a couple of ways to create a Series from them.
Using a dict
x = dict(zip([x for x in 'abcdefg'], xrange(1, 8))) print x y = pd.Series(x) print y # prints {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6} a 1 b 2 c 3 d 4 e 5 f 6 g 7 dtype: int64
Specifying an index
Skip defining a dict and create the Series directly.
print pd.Series(xrange(1,8), index=[x for x in 'abcdefg']) # prints a 1 b 2 c 3 d 4 e 5 f 6 g 7 dtype: int64
7. Naming a Series
You can also assign a name to the Series. When used in the context of a DataFrame (to be covered next), this name serves as the column name.
a = [1, 3, 5, 7] print pd.Series(a, name='joe') # prints 0 1 1 3 2 5 3 7 Name: joe, dtype: int64
8. Comparing List with Series
A Series is like a list. The usual array indexing works as expected, as does the array slicing. Notice that the slice operator returns a Series itself.
ser = pd.Series(random.sample(xrange(100), 10)) print ser print print '4th element: ', ser[4] print 'Slice: ', ser[3:8] # prints 0 79 1 4 2 71 3 20 4 19 5 24 6 82 7 74 8 17 9 48 dtype: int64 4th element: 19 Slice: 3 20 4 19 5 24 6 82 7 74 dtype: int64
8.1. Using a Predicate for Extraction
While list elements can be extracted using indexing and slicing, Series elements can also be extracted using a predicate function (a function returning True or False) as shown in this example.
x = random.sample(range(100), 10) print x y = pd.Series(x) print y[y > 30] # prints [22, 5, 16, 56, 38, 48, 64, 41, 71, 63] 3 56 4 38 5 48 6 64 7 41 8 71 9 63 dtype: int64
8.2. Indexing Using a List
In addition to extraction using a predicate function, items can also be retrieved from a Series using a list as the index.
x = random.sample(xrange(100), 10) print x y = pd.Series(x) print y[[2, 0, 1, 2]] # prints [29, 1, 27, 54, 2, 90, 25, 96, 45, 34] 2 27 0 29 1 1 2 27 dtype: int64
8.3. Executing a Function on a Series
Unlike a list, a function accepting a scalar argument can be invoked with a Series. The result will be another Series with the function applied to each element of the Series. This allows more flexible and concise ways in which operations can be combined. Just what is needed in a data analysis toolkit.
def joe(x): return x + 10 x = random.sample(xrange(100), 10) print 'Data => ', x, '\n' y = pd.Series(x) print 'Applying pow => \n', pow(y, 2), '\n' print 'Applying joe => \n', joe(y) # prints Data => [96, 63, 79, 11, 49, 41, 12, 26, 20, 62] Applying pow => 0 9216 1 3969 2 6241 3 121 4 2401 5 1681 6 144 7 676 8 400 9 3844 dtype: int64 Applying joe => 0 106 1 73 2 89 3 21 4 59 5 51 6 22 7 36 8 30 9 72 dtype: int64
9. Comparing a Dict with a Series
A Series also behaves like a python dictionary.
9.1. Indexing with Label
For example, you can extract items using the index label.
x = pd.Series(xrange(1,8), index=[x for x in 'abcdefg']) print x, '\n' print 'element "d" => ', x['d'] # prints a 1 b 2 c 3 d 4 e 5 f 6 g 7 dtype: int64 element "d" => 4
9.2. Checking for Membership
Use the “in” operator to check whether a Series contains a specific label.
x = pd.Series(xrange(1,8), index=[x for x in 'abcdefg']) print x, '\n' print 'is "j" in x?', 'j' in x print 'is "d" in x?', 'd' in x # prints a 1 b 2 c 3 d 4 e 5 f 6 g 7 dtype: int64 is "j" in x? False is "d" in x? True
Summary
This article was a gentle introduction to pandas Series. A Series can be constructed using a plain list, a dict or a scalar. Many operations on a Series allow concise expression and are useful for powerful data analysis and munging.