Python Pandas Series pow() Function

Pandas Series pow() Function:

The pow() method of the Pandas module returns the element-wise exponential power of series and other. It’s the same as series ** other, but with the ability to provide a fill_value as one of the parameters to fill in missing data.

Syntax:

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

Parameters

other: This is required. It indicates a Series or a scalar value.

level: This is optional. To broadcast over a level, specify an int or a name that matches the Index values on the passed MultiIndex level. The type of this may be int or name. None is the default value.

fill_value: This is optional. It indicates a value to fill in any current missing (NaN) values, as well as any new elements required for successful Series alignment. The result will be missing if data is missing in both corresponding Series locations. The type of this may be float or None. None is the default value.

Return Value:

The result of the arithmetic operation is returned.

Pandas Series pow() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Apply pow() function on all the elements of the given series by passing some random number to it and print the result.
  • Here it gives the cubes of all the elements of the given series.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([5, 2, 3, 1, 4])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply pow() function on all the elements of the given series
# by passing some random number to it and print the result.
# Here it gives the cubes of all the elements of the given series 
print("The cubes of all the elements of the given series:")
print(gvn_series.pow(3))

Output:

The given series is:
0    5
1    2
2    3
3    1
4    4
dtype: int64

The cubes of all the elements of the given series:
0    125
1      8
2     27
3      1
4     64
dtype: int64

Example2

Here, each element of the first series is raised to the power of the corresponding element of the second series

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random list, index values as the arguments to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Similarly, Pass some random list, index values as the arguments to the Series() function of the pandas module to create another series.
  • Store it in another variable.
  • Print the above first given series
  • Print the above second given series
  • Pass the given second series and fill_value as some random number as the arguments to the pow() function and apply it to the first series and print the result.
  • Here, each element of the first series is raised to the power of the corresponding element of the second series by filling NaN values with the given fill_value.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random list, index values as the arguments to the 
# Series() function of the pandas module to create a series.
# Store it in a variable.
gvn_series1 = pd.Series([2, 5, 4, np.NaN], 
              index=['P', 'Q', 'R', 'S'])
# Similarly, Pass some random list, index values as the arguments to the 
# Series() function of the pandas module to create another series.
# Store it in another variable.
gvn_series2 = pd.Series([5, 3, np.NaN, 2], 
              index=['P', 'Q', 'R', 'S'])
# Print the above first given series
print("The given first series is:")
print(gvn_series1)
print()
# Print the above second given series
print("The given second series is:")
print(gvn_series2)
print()
# Pass the given second series and fill_value as some random number as the 
# arguments to the pow() function and apply it to the first series and
# print the result.
# Here, each element of the first series is raised to power of the corresponding element
# of the second series by filling NaN values with the given fill_value.
print("Result when first series is raised to power of second series element-wise:")
print(gvn_series1.pow(gvn_series2, fill_value=10))

Output:

The given first series is:
P    2.0
Q    5.0
R    4.0
S    NaN
dtype: float64

The given second series is:
P    5.0
Q    3.0
R    NaN
S    2.0
dtype: float64

Result when first series is raised to power of second series element-wise:
P         32.0
Q        125.0
R    1048576.0
S        100.0
dtype: float64

Example3

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary) as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Get the result when given base_values are raised to the power of given exponent_values using the pow() function and store it as a new column in the dataframe.
  • Print the dataframe after adding a new column(base_values**exponent_values)
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary) as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme  = pd.DataFrame({
  "base_values": [5, 2, 3, 4, 10],
  "exponent_values": [3, 6, 2, 3, 5]
})
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Get the result when given base_values are raised to the power of
# given exponent_values using the pow() function and 
# store it as a new column in the dataframe.
data_frme ['base_values**exponent_values'] = data_frme ['base_values'].pow(data_frme ['exponent_values'])
# Print the dataframe after adding a new column(base_values**exponent_values)
print("The dataframe after adding a new column(base_values**exponent_values):")
print(data_frme )

Output:

The given Dataframe:
   base_values  exponent_values
0            5                3
1            2                6
2            3                2
3            4                3
4           10                5

The dataframe after adding a new column(base_values**exponent_values):
   base_values  exponent_values  base_values**exponent_values
0            5                3                           125
1            2                6                            64
2            3                2                             9
3            4                3                            64
4           10                5                        100000