Multiplying A Pandas Series With Another Series

Overview:

  • The Series class of Python pandas library, implements a one-dimensional container suitable for data-analysis such as analyzing time-series data.
  • Series class is built with numpy.ndarray as its underlying storage.
  • Series class is designed as a mutable container, which means elements, can be added or removed after construction of a Series instance. The number of elements in a Series can grow or shrink dynamically at runtime.
  • A Series instance can have elements of different types. First element of the Series can be an integer, second element can be a floating point number and so on.
  • Multiplication is one of the binary operations (dyadic operations) supported by the pandas Series class along with Addition, Subtraction, Division, dot product, greater than, less than, greater than or equal to, less than or equal to, equal to and not equal to operations.

 

Multiplying a pandas Series with another Series:

  • The mul() method of the pandas Series multiplies the elements of one pandas Series with another pandas Series returning a new Series.
  • Multiplying of two pandas.Series objects can be done through applying the multiplication operator “*” as well.
  • Through mul() method, handling None values in the data is possible by replacing them with a default value using the parameter fill_value.
  • In the same way, to multiply one DataFrame with another DataFrame, the mul() function of DataFrame class can be used.

Example – Multiply two pandas Series instances:

# Example program to multiply two pandas series instances

import pandas as pds

 

# Create Series instances

priceSeries         = pds.Series([1.1, 4.3, 2.2, 7.41, 2.89]);

quantitySeries      = pds.Series([100, 15, 50, 15, 25]);

 

# Multiply the pandas  Series instances

costBeforeTax       = priceSeries.mul(quantitySeries);

 

print("Price Series:");

print(priceSeries);

 

print("Quantity Series:");

print(quantitySeries);

 

print("Series representing cost before tax:");

print(costBeforeTax);

 

 

Output:

Price Series:

0    1.10

1    4.30

2    2.20

3    7.41

4    2.89

dtype: float64

Quantity Series:

0    100

1     15

2     50

3     15

4     25

dtype: int64

Series representing cost before tax:

0    110.00

1     64.50

2    110.00

3    111.15

4     72.25

dtype: float64

 

Example:

While multiplying two pandas Series instances any occurrence of None can be replaced with a default value using the parameter fill_value.

# Example Python program to multiply two python Series objects

# while replacing any None value with a default value before multiplication

import pandas as pds

 

# Create pandas Series instances

series1 = pds.Series([15, 34, 65, 111, 175]);

series2 = pds.Series([None, 1, 1.5, 2, None]);

 

# Multiply series1 and series2 specifying the fill_value for None

series3 = series1.mul(series2, fill_value=1);

 

print("Series1:");

print(series1);

 

print("Series2:");

print(series2);

 

print("Series1 * Series2:");

print(series3);

 

 

Output:

Series1:

0     15

1     34

2     65

3    111

4    175

dtype: int64

Series2:

0    NaN

1    1.0

2    1.5

3    2.0

4    NaN

dtype: float64

Series1 * Series2:

0     15.0

1     34.0

2     97.5

3    222.0

4    175.0

dtype: float64

 

Example:

A pandas Series can be multiplied with any Python Sequence such as a list, tuple. In this Python example code, a pandas Series is multiplied with a one-dimensional numpy.ndarray.

Example:

# Example Python program to multiply an ndarray and a pandas Series

import pandas as pds

import numpy as np

 

# Create an one-dimensional ndarray

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

print(type(randomNumbers));

 

# Create a pandas Series

series = pds.Series([0.1, 0.2, 0.3, 0.4, 0.5]);

 

# Multiply a pandas Series with an ndarray

resultant = series.mul(randomNumbers);

 

print("Contents of the ndarray");

print(randomNumbers);

 

print("Contents of the pandas Series");

print(series);

 

print("Result of multiplying the pandas series with an ndarray:")

print(resultant);

 

Output:

<class 'numpy.ndarray'>

Contents of the ndarray

[ 2 14  9 12 16]

Contents of the pandas Series

0    0.1

1    0.2

2    0.3

3    0.4

4    0.5

dtype: float64

Result of multiplying the pandas series with an ndarray:

0    0.2

1    2.8

2    2.7

3    4.8

4    8.0

dtype: float64

 


Copyright 2022 © pythontic.com