Subtracting a Pandas Series Object from another

Subtracting a pandas.Series instance from another produces a new Series by subtracting each element of the series from the corresponding element of the another series. The pandas example code uses sub() to subtract and prints the results.

Series Binaryoperatorfunctions Updated 25 Aug 2019 3 min read

Overview

  • The Series class of the python library pandas, implements a one-dimensional, mutable container for storing heterogeneous elements using the numpy.ndarray as the underlying storage.
  • In addition to providing a versatile one dimensional container, the pandas.Series provides numerous computational methods for analyzing the data.
  • The subtraction operation is a binary operation. A binary operation consumes two values to produce a new value. In case of subtraction between two pandas.Series instances, one element of the Series is subtracted from the another producing a new Series.
  • To subtract two pandas.Series instances, the function Series.sub(**)** is used. The subtraction operator “–“ can as well be used for the same purpose. In the similar way to subtract a DataFrame instance from another, the DataFrame.sub() function can be used.
  • While using the sub() function explicitly, it is possible to replace any occurrence of None, using the parameter fill_value.

Example

python
# Example Python program that subtracts one series from another

import pandas as pds

import numpy as np

# Generate ten random integers between 1 and 20

np.random.seed(1);

randomNumbers = np.random.randint(1, 20, 10);

# Create a pandas series

series1 = pds.Series(randomNumbers);

# Generate ten random integers between 1 to 5

np.random.seed(1);

randomNumbers = np.random.randint(1, 5, 10);

# Create a pandas series

series2 = pds.Series(randomNumbers);

# Subtract a pandas Series instance from another

series3 = series1.sub(series2);

print("Series1:");

print(series1);

print("Series2:");

print(series2);

print("Series3:");

print(series3);

Output

Output
Series1:

0     6

1    12

2    13

3     9

4    10

5    12

6     6

7    16

8     1

9    17

dtype: int64

Series2:

0    2

1    4

2    1

3    1

4    4

5    2

6    4

7    2

8    4

9    1

dtype: int64

Series3:

0     4

1     8

2    12

3     8

4     6

5    10

6     2

7    14

8    -3

9    16

dtype: int64

Example - Subtracting a pandas Series from another while replacing None values with zero

python
# Example Python program that subtracts one series from another

# where one or more elements are None.

import pandas as pds

import numpy as np

# Create pandas.Series instances

lookAndSaySeries = pds.Series([1, 11, 21, 1211, 111221, 312211]);

series2Subtract  = pds.Series([1, 0, None, 2, None, 4]);

# While subtracting replace any None with zero

newSeries        = lookAndSaySeries.sub(series2Subtract, fill_value=0);

print("Look and Say Series:");

print(lookAndSaySeries);

print("Series to Subtract:");

print(series2Subtract);

print("New series after subtraction:");

print(newSeries);

Output

Output
Look and Say Series:

0         1

1        11

2        21

3      1211

4    111221

5    312211

dtype: int64

Series to Subtract:

0    1.0

1    0.0

2    NaN

3    2.0

4    NaN

5    4.0

dtype: float64

New series after subtraction:

0         0.0

1        11.0

2        21.0

3      1209.0

4    111221.0

5    312207.0

dtype: float64

Example

Any python sequence of same length like a Python tuple, a Python list can be subtracted from a pandas.Series, as given in the Python example program below.

python
# Example Python program that subtracts the elements

# of a tuple from a pandas.Series

import pandas as pds

import numpy as np

numbers = [1, 1, 4, 8, 22, 51];

# Create a pandas.Series from a Python List

series1 = pds.Series(numbers);

# Create a python tuple

toSubtract = (1, 2, 3, 4, 9, 20);

# Subtract a tuple from a series

resultantSeries = series1.sub(toSubtract);

print("Contents of the pandas.Series instance:");

print(series1);

print("Contents of the Python tuple:");

print(series1);

print("Resultant pandas.Series after subtracting the tuple:");

print(resultantSeries);

Output

Output
Contents of the pandas.Series instance:

0     1

1     1

2     4

3     8

4    22

5    51

dtype: int64

Contents of the Python tuple:

0     1

1     1

2     4

3     8

4    22

5    51

dtype: int64

Resultant pandas.Series after subtracting the tuple:

0     0

1    -1

2     1

3     4

4    13

5    31

dtype: int64