Table of Contents



Introduction

A Series object behaves in many ways like a 1-D NumPy array and in many ways like a standard Python dictionary. This section focuses on means of accessing and modifying values in a pandas Series.

We first import the required libraries and modules.

1import numpy as np
2import pandas as pd
3import random as rd

Series as a Dictionary

Similar to a Python dictionary, the Series object provides a mapping from a collection of keys to a collection of values.

Example

Creating a pandas Series with explicit indexing.

1x = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
2x
a    1
b    2
c    3
d    4
dtype: int64

Access a Value by Index

We can access individual elements (values) of a Series by using its index (key).

Example

Selecting elements of a pandas Series with explicit index.

1x['a']
1

Access all Indices

We can use either the index attribute or the keys() method to access the indices or keys.

Example

Printing out all indices (or keys) using index attribute.

1x.index
Index(['a', 'b', 'c', 'd'], dtype='object')

Example

Printing out all indices (or keys) using keys() method.

1x.keys()
Index(['a', 'b', 'c', 'd'], dtype='object')

Access all Values

We can access the values of an index object using the values attribute.

Example

Printing out all values using values attribute.

1x.values
array([1, 2, 3, 4], dtype=int64)

The list constructor applied to a Series object produces a list of its values.

Example

Converting a Series to a Python list using the list() constructor.

1list(x)
[1, 2, 3, 4]

Access all Index-Value Pairs as a List

The list constructor applied to x.items() produces a list of key-value tuples.

Example

Access all Index-Value pairs of a Series using items() method.

1list(x.items())
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Extending a Series

Just as we can extend a dictionary by assigning a value to a new key, we can extend a Series by assigning a value to a new index.

Example

Extending a Series by assigning a value to a new index.

1x['e'] = 5
2x
a    1
b    2
c    3
d    4
e    5
dtype: int64

Series as a One-dimensional Array

A Series provides NumPy array-style item selection via slicing, masking, and fancy indexing.

Note

There are two ways of slicing a Series:

  • Explicit index: value at end-index included.
  • Implicit index: value at end-index not included.

Slicing by Explicit Index

Example

Slicing a Series by explicit index (or keys).

1x['b':'d']
b    2
c    3
d    4
dtype: int64

Slicing by Implicit Integer Index

Example

Slicing a Series by implicit index.

1x[1:4]
b    2
c    3
d    4
dtype: int64

Selection by Boolean Indexing

We can filter a Series based on specified Boolean expressions.

Example

Filtering a Series by Boolean masking.

In this example, we want all x such that x is greater than 2.

1x[x > 2]
c    3
d    4
e    5
dtype: int64

Example

Filtering a Series by Boolean masking.

In this example, we want all x such that x is greater than 2 and less than 4.

1x[(x > 2) & (x < 4)]
c    3
dtype: int64

Example

Filtering a Series by Boolean masking.

In this example, we want all x such that x is divisible by 2.

1x[x%2==0]
b    2
d    4
dtype: int64

Fancy Indexing

Fancy indexing means passing an array of indices to access multiple array elements at once.

Example

Explicit fancy indexing.

1x[['a', 'c']] # explicit index
a    1
c    3
dtype: int64

Example

Implicit fancy indexing.

1ind = [0,3,4] # integer index
2x[ind]
a    1
d    4
e    5
dtype: int64

Series Explicit vs Implicit Index

Caution

If a Series has an explicit integer index,

  • indexing operation will use the explicit indices.
  • slicing operation will use the implicit integer index.

Example

Create a Series with a random explicit integer index.

1y = pd.Series('a b c d e'.split(), index=rd.sample(range(1,6),5)  )
2y
4    a
5    b
1    c
2    d
3    e
dtype: object

Example

Indexing makes use of explicit index.

1y[1]
'c'

Example

Slicing makes use of implicit integer index.

1y[1:4]
5    b
1    c
2    d
dtype: object

Because of the above potential confusion in the case of explicit integer indices, pandas provides two special indexer attributes (loc and iloc) which we will discuss next.

The loc Attribute

The loc attribute allows indexing and slicing that always references the explicit index (i.e. end-index inclusive).

1y.loc[1]
'c'
1y.loc[5:2]
5    b
1    c
2    d
dtype: object

The iloc Attribute

The iloc attribute allows indexing and slicing that always refer to the implicit integer index (i.e. end-index not inclusive).

1y.iloc[1]
'b'
1y.iloc[1:4]
5    b
1    c
2    d
dtype: object