Access Elements of Pandas Series

Let's learn how to access the values of a pandas series.

We'll cover the following

Try it yourself

Try executing the code below to see the result.

Press + to interact
import pandas as pd
simpsons = pd.Series(
['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie'])
print('Bart' in simpsons)

Explanation

The sequence type is pandas.Series. Most Python sequences are indexed by a range, meaning the first item is at index 0, the second item is at index 1, and so on.

Note about 0 vs. 1: Python is a 0-based language. Some languages, such as MATLAB, are 1-based. In this puzzle, the compromise of using ½½ as the first index didn’t go well.

The pandas.Series (and pandas.DataFrame) indices are more flexible than Python’s lists. The default is a range-based index, but there are other types of indices as well.

In [1]: import pandas as pd
In [2]: pd.Series([1,2,3,4], index=['a', 'b', 'b', 'c'])
Out[2]:
a 1
b 2
b 3
c 4
dtype: int64

The previous example index has strings as labels.

Note: The labels don’t have to be unique.

Press + to interact
import pandas as pd
a = pd.Series([1,2,3,4], index=['a', 'a', 'b', 'c'])
print(a)
In [3]: pd.Series([1,2,3,4], index=pd.date_range('2020', periods=4))
Out[3]:
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
Freq: D, dtype: int64

This series has a pandas.DatetimeIndex index. Indexing with pandas.DatetimeIndex enables a lot of time series operations, such as upsampling, downsampling, and more.

These kinds of indices make a pandas.Series behave like a dict as well.

In [4]: s = pd.Series([1,2,3], index=['a', 'b', 'c'])
In [5]: s['c']
Out[5]: 3

This allows two choices for the in operator. The in can either behave like a sequence (for example a list or a tuple) or like a dict. The design choice in this example has in behave like a dict and look through the keys that are the index labels.

⚠️ Note

When it comes to performance, the in operator of pandas.Series is very slow compared to the built-in dict.

To check if a pandas.Series contains a value, we use .values. The .values property returns the underlying NumPy array and the in operator works as expected.

Solution

Press + to interact
import pandas as pd
simpsons = pd.Series(
['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie'])
print('Bart' in simpsons.values)

Get hands-on with 1300+ tech skills courses.