Raising the elements of a pandas Series to the power given by the elements of another pandas Series

Overview:

  • The Series.pow() function when called on a pandas Series instance raises its elements to the power given by another series.
  • Calling Series.pow() is equivalent to using the exponential operator (**). However, the pow() function enables to replace the NaN/None values by a default value through fill_value parameter.

Example - Raise the elements of one pandas Series to the power given by the elements of another pandas Series:

# An example Python program to raise the elements of a pandas Series

# to the power given by the elements of another Series

import pandas as pds

 

# Create pandas Series instances

series1 = pds.Series([1, 2, 3, 4, 6, 8]);

series2 = pds.Series([2, 4, 6, 8, 10, 12]);

 

# Raise the first series to the power given by the second series

result  = series1.pow(series2);

 

print("Elements of the first Series:");

print(series1);

 

print("Elements of the second Series:");

print(series2);

 

print("Elements of the first Series raised to the power by the elements of the second series:")

print(result);

 

Output:

Elements of the first Series:

0    1

1    2

2    3

3    4

4    6

5    8

dtype: int64

Elements of the second Series:

0     2

1     4

2     6

3     8

4    10

5    12

dtype: int64

Elements of the first Series raised to the power by the elements of the second series:

0              1

1             16

2            729

3          65536

4       60466176

5    68719476736

dtype: int64

 

Example – Applying pow() on a pandas Series using a Python Sequence:

This example calls Series.pow() on a pandas Series instance using a Python list as a parameter.

# Example python program that uses Series.pow() function

# to raise elements of one pandas series to the power

# defined by the elements of a Python Sequence

import pandas as pds

 

# Create a pandas Series

pandasSeries = pds.Series([5, 10, 15, 20, 25]);

 

# Creata a Python List

pythonList   = [2, 4, 8, 12, 16];

 

# Raise elements of the series by the values from the Python list

result = pandasSeries.pow(pythonList);

 

print("Elements of the pandas Series:");

print(pandasSeries);

 

print("Elements of the Python list:");

print(pythonList);

 

print("Elements of pandas Series raised to the power of elements from the Python list:");

print(result);

 

Output:

Elements of the pandas Series:

0     5

1    10

2    15

3    20

4    25

dtype: int64

Elements of the Python list:

[2, 4, 8, 12, 16]

Elements of pandas Series raised to the power of elements from the Python list:

0                     25

1                  10000

2             2562890625

3       4096000000000000

4    3273344365508751233

dtype: int64

 

Example – First Series as base and the second Series as exponent while replacing the NaN/None values with default values:

The following Python example program uses two pandas Series instances. The elements of the first pandas Series are used as base values. The elements of the second Series are used as exponent values. The Series.pow() is called on the first Series by using the second Series as the parameter. The fill_value parameter repalces any NaN/None values with default values. A new series is returned by Series.pow(), which has the series1 elements raised to the power given by the series2 elements. 

# Using the elements of a pandas Series as base

# and using the elements of another pandas Series as exponent

# while replacing NaN/None values with the default values

import pandas as pds

 

# Create two pandas.Series instances

s1 = pds.Series([0.25, 0.5, 0.75, 1.0, 1.25]);

s2 = pds.Series([2, None, 3, 4, None]);

 

# Raise s1 elements, using the elements from s2 as exponents

s3 = s1.pow(s2, fill_value=2);

 

print("s1:");

print(s1);

 

print("s2:");

print(s2);

 

print("s1 elements raised to power by the elements of s2:");

print(s3);

 

Output:

s1:

0    0.25

1    0.50

2    0.75

3    1.00

4    1.25

dtype: float64

s2:

0    2.0

1    NaN

2    3.0

3    4.0

4    NaN

dtype: float64

s1 elements raised to power by the elements of s2:

0    0.062500

1    0.250000

2    0.421875

3    1.000000

4    1.562500

dtype: float64

 


Copyright 2024 © pythontic.com