This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: Series

Accessing Series elements

face Josiah Wang

Accessing elements in a Series is straightforward.

You can access elements in a Series just like in a NumPy array. Slicing also works.

>>> series = pd.Series(data=["UK", "France", "Italy"])
>>> print(series)
0        UK
1    France
2     Italy
dtype: object
>>> print(series[0])  
UK
>>> print(series[1:])
1    France
2     Italy
dtype: object

You can also access elements by their index labels.

>>> series = pd.Series(data={"a": "UK", "b": "France", "c": "Italy"})
>>> print(series)
a        UK
b    France
c     Italy
dtype: object
>>> print(series["a"])
UK
>>> print(series[["c", "b"]])
c     Italy
b    France
dtype: object

If you need a NumPy array representation of your data, you can access it using the series.to_numpy() method. To convert it to a Python list, you can use list(series) or series.tolist().

>>> series = pd.Series(data={"a": "UK", "b": "France", "c": "Italy"})
>>> series_array = series.to_numpy()
>>> print(type(series_array))
<class 'numpy.ndarray'>
>>> print(series_array)
['UK' 'France' 'Italy']
>>> series_list = list(series)  # or series.tolist()
>>> print(type(series_list))
<class 'list'>
>>> print(series_list)
['UK', 'France', 'Italy']

We will not spend any more time discussing Series. I believe that you are already well-versed enough to read the documentation yourself and see what attributes/methods are available for Series instances.