Signup/Sign In
PUBLISHED ON: MARCH 25, 2021

Pandas DataFrame pow() Method

We can find the exponent or power of a number that is a^b (a power of b) using one of the DataFrame methods.

In this tutorial, we will discuss and learn the Python pandas DataFrame.pow() method. This method can be used to get the exponential power of the specified DataFrame and others. This method returns a DataFrame which consists of the power of a number.

The below is the syntax of the DataFrame.pow() method.

Syntax

DataFrame.pow(other, axis='columns', level=None, fill_value=None)

Parameters

other: It can be a scalar, sequence, Series, or DataFrame. It can be a single or multiple element data structure, or list-like object.

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis. For the input Series , axis to match Series index on.

level: It represents an int or label. It broadcasts across a level, matching Index values on the passed MultiIndex level.

fill_value: It represents the float or None, the default value is None. It fills the existing missing or null values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Example 1: Getting the Power of the DataFrame Values

In the below example, we are getting the exponential power of the DataFrame with a scalar using the DataFrame.pow() method that returns a DataFrame. It consists of the exponential power values of the DataFrame. See the example below.

For instance, a^b. a is the DataFrame values and b is the scalar.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print("------The DataFrame is---------")
print(df_1)
print("---------------------------------")
print(df_1.pow(2))


------The DataFrame is---------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
---------------------------------
A B C
0 1 16 49
1 4 25 64
2 9 36 81

Example 2: Getting the Power of the DataFrame Values

In the below example, we are getting the exponential power of the DataFrame values with the other DataFrame using the DataFrame.pow() method that returns a DataFrame consisting of the exponential power values of DataFrame. See the example below.

For instance, a^b. a is the DataFrame values and b is the other DataFrame values.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,1,1],'B':[2,2,2],'C':[3,3,3]})
df_2=pd.DataFrame({'A':[1,2,3],'B':[1,2,3],'C':[1,2,3]})
print("---------------------------------")
print(df_1.pow(df_2,axis=0))


A B C
0 1 2 3
1 1 4 9
2 1 8 27

Example 3: Getting the Power of the DataFrame Values

Here, we are getting power of the DataFrame with a Series using the DataFrame.pow() method that returns a DataFrame that consists of the exponential power values of DataFrame. See the example below.

For instance, a^b. a is the DataFrame values and b is the Series.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print("------The DataFrame is---------")
print(df_1)
series=pd.Series([2,2,2])
print("---------------------------------")
print(df_1.pow(series,axis=0))


------The DataFrame is---------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
---------------------------------
A B C
0 1 16 49
1 4 25 64
2 9 36 81

Example 4: Getting the Power of the DataFrame Values

Here, we are getting the power of the DataFrame with a sequence using the DataFrame.pow() method that returns a DataFrame that consists of the exponential power values of DataFrame. See the example below.

For instance, a^b. a is the DataFrame values and b is the sequence values.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[2,3,4],'B':[5,7,8],'C':[10,11,0]})
print("------The DataFrame is---------")
print(df_1)
tuple_1=(2,2,2)
print("---------------------------------")
print(df_1.pow(tuple_1))


------The DataFrame is---------
A B C
0 2 5 10
1 3 7 11
2 4 8 0
---------------------------------
A B C
0 4 25 100
1 9 49 121
2 16 64 0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.pow() method. We learned syntax, parameters, and solved examples by applying this method on the DataFrame and understood the method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.