Python | Pandas Series.pow()
Last Updated :
30 Sep, 2019
Improve
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
Python3 1==
Output:
As shown in the output, the returned values are equal to first series with second series as its exponential power.
Python3 1==
Output:
As shown in the output, all NaN values were replaced by 2 before the operation and result was returned without any Null value in it.
Series.pow()
is a series mathematical operation method. This is used to put each element of passed series as exponential power of caller series and returned the results. For this, index of both the series has to be same otherwise error is returned.
Syntax: Series.pow(other, =None, fill_value=None, axis=0) Parameters: other: Other series or list type to be put as exponential power to the caller series level: Value to be replaced by NaN in series/list before operation fill_value: Integer value of level in case of multi index Return : Value of caller series with other series as its exponential powerExample #1: In this example, two Series are created using Pandas .Series() method. None of the series is having null values. The second series is directly passed as other parameter to return the values after operation.
# importing pandas module
import pandas as pd
# creating first series
first =[1, 2, 5, 6, 3, 4]
# creating second series
second =[5, 3, 2, 1, 3, 2]
# making series
first = pd.Series(first)
# making series
second = pd.Series(second)
# calling .pow()
result = first.pow(second)
# display
result
0 1 1 8 2 25 3 6 4 27 5 16 dtype: int64Example #2: Handling Null values In this example, NaN values are also put in the series using numpy.nan method. After that 2 is passed to fill_value parameter to replace null values with 2.
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# creating first series
first =[1, 2, 5, 6, 3, np.nan, 4, np.nan]
# creating second series
second =[5, np.nan, 3, 2, np.nan, 1, 3, 2]
# making series
first = pd.Series(first)
# making seriesa
second = pd.Series(second)
# value for null replacement
null_replacement = 2
# calling .pow()
result = first.pow(second, fill_value = null_replacement)
# display
result
0 1.0 1 4.0 2 125.0 3 36.0 4 9.0 5 2.0 6 64.0 7 4.0 dtype: float64