Python | Pandas Series.pow()

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 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 power

Example #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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:
As shown in the output, the returned values are equal to first series with second series as its exponential power.

0     1
1     8
2    25
3     6
4    27
5    16
dtype: int64

 
Example #2: Handling Null values

In this exmaple, 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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.

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


My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.