Pandas Tutorial Pandas References

Pandas DataFrame - pow() function



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

Syntax

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

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label 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 DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: using pow() on whole DataFrame

In the example below, a DataFrame df is created. The pow() function is used to calculate the square of the whole DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Squaring all entries of the DataFrame
print("\ndf.pow(2) returns:")
print(df.pow(2))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.pow(2) returns:
       Bonus  Salary
John      25    3600
Marry      9    3844
Sam        4    4225
Jo        16    3481

Example: Different exponent for different column

Different exponent can be used for different column by providing other argument as a list. Consider the following example:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Cubing all entries of Bonus column
#Squaring all entries of Salary column
print("\ndf.pow([3,2]) returns:")
print(df.pow([3,2]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.pow([3,2]) returns:
       Bonus  Salary
John     125    3600
Marry     27    3844
Sam        8    4225
Jo        64    3481

Example: using pow() on selected columns

Instead of whole DataFrame, the pow() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Squaring all entries of Salary column
print("\ndf['Salary'].pow(2) returns:")
print(df["Salary"].pow(2))

#Cubing all entries of Bonus column
#Squaring all entries of Salary column
print("\ndf[['Salary', 'Bonus']].pow([2,3]) returns:")
print(df[["Salary", "Bonus"]].pow([2,3]))

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].pow(2) returns:
John     3600
Marry    3844
Sam      4225
Jo       3481
Name: Salary, dtype: int64

df[['Salary', 'Bonus']].pow([2,3]) returns:
       Salary  Bonus
John     3600    125
Marry    3844     27
Sam      4225      8
Jo       3481     64

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 DataFrame - Functions