Pandas Tutorial Pandas References

Pandas Series - pow() function



The Pandas pow() function returns exponential power of series and other, element-wise. It is equivalent to series ** other, but with support to substitute a fill_value for missing data as one of the parameters.

Syntax

Series.pow(other, level=None, fill_value=None)

Parameters

other Required. Specify scalar value or Series.
level Optional. Specify int or name to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful Series alignment. If data in both corresponding Series locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: Squaring the Series

In the example below, the pow() function is used to calculate the square of the series.

import pandas as pd
import numpy as np

x = pd.Series([10, 20, 30, 40, 50])

print("The Series contains:")
print(x)

#Squaring the series
print("\nx.pow(2) returns:")
print(x.pow(2))

The output of the above code will be:

The Series contains:
0    10
1    20
2    30
3    40
4    50
dtype: int64

x.pow(2) returns:
0     100
1     400
2     900
3    1600
4    2500
dtype: int64

Example: Calculating exponential using two Series

Two series can be used to calculate exponent of the series element-wise. Consider the following example:

import pandas as pd
import numpy as np

x = pd.Series([15, np.NaN, 11, 9, 7], 
              index=['A', 'B', 'C', 'D', 'E'])
y = pd.Series([1, 2, 3, np.NaN, 5], 
              index=['A', 'B', 'C', 'D', 'E'])

print("The x contains:")
print(x)

print("\nThe y contains:")
print(y)

#calculating x raised to the power of y
print("\nx.pow(y, fill_value=0) returns:")
print(x.pow(y, fill_value=0))

The output of the above code will be:

The x contains:
A    15.0
B     NaN
C    11.0
D     9.0
E     7.0
dtype: float64

The y contains:
A    1.0
B    2.0
C    3.0
D    NaN
E    5.0
dtype: float64

x.pow(y, fill_value=0) returns:
A       15.0
B        0.0
C     1331.0
D        1.0
E    16807.0
dtype: float64

Example: Calculating pow() using columns of a DataDrame

The pow() function can be applied in a DataFrame to get the exponential of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Base": [10, 20, 30, 40, 50],
  "Exponent": [1.1, 1.2, 1.3, 1.4, 1.5]
})

print("The DataFrame is:")
print(df)

#calculating 'Base' raised to the power of 'Exponent'
df['Result'] = df['Base'].pow(df['Exponent'])

print("\nThe DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
   Base  Exponent
0    10       1.1
1    20       1.2
2    30       1.3
3    40       1.4
4    50       1.5

The DataFrame is:
   Base  Exponent      Result
0    10       1.1   12.589254
1    20       1.2   36.411284
2    30       1.3   83.225733
3    40       1.4  174.937932
4    50       1.5  353.553391

❮ Pandas Series - Functions

5