In Python, the pandas DataFrame.pow()
function is used to calculate the exponential power of DataFrame
and other types of objects. The other type of objects can be sequence, series, constant, and more. It is equivalent to the DataFrame ** other
operation.
Note: The**
operator can also be used, but it does not provide the facility to fill out empty fields, broadcast values across a level, and so on.
The syntax of the function is given below:
DataFrame.pow(other, axis='columns', level=None, fill_value=None)
The DataFrame.pow()
function takes the following argument values:
other
: This can be DataFrame
, sequence, series, or scalar.axis
: defaut=
'columns'
, this can be 1
, 'columns'
, 0
, or 'index'
.level
: default=
None
, this broadcasts value across a level and can either be int
or None
.fill_value
: default=
None
, this can either be a float
value or None
, and it helps to fill empty (NaN
) fields.This method returns a DataFrame
after performing certain
We can use this function in multiple ways. Some of the scenarios where we use this function are listed below:
DataFrames
DataFrame
and a constant DataFrame
and seriesDataFrames
Let's say we have two objects that are both DataFrames
:
# importing pandas as pdimport pandas as pd# Creating a the DataFramedf1= pd.DataFrame({"A":[5, 9, 6, 4],"B":[1, None, 4, 3],"C":[4, 3, 8, None],"D":[5, 4, 2, 7]})# Creating another DataFramedf2= pd.DataFrame({"A":[None, 4, 5, 9],"B":[1, None, 4, 3],"C":[14, 3, -1, None],"D":[2, 4, 8, 8]})# Evaluating floor division between df1 and df2floorDiv= df1.pow(df2, fill_value = 3)# print resultsprint(floorDiv)
DataFrame
named df1
, which has four rows and four columns.DataFrame
named df2
, which has the same dimensions as df1
.DataFrame.pow()
function from the pandas module to calculate, element-wise, the exponential power between the DataFrames
that we created.DataFrame
and constantLet's say we have two objects, where one is a DataFrame
and another is a constant:
# importing pandas as alias pdimport pandas as pd# Creating a DataFramedf = pd.DataFrame({"Alpha":[15, 3, 16, 8],"Beta":[1, None, 4, 13],"Charli":[4, 3, 8, None],"Delta":[5, 1, 12, 8]})# Invoking method for floor divisionresult= df.pow(4, fill_value = 5)# print results on consoleprint(result)
DataFrame
named df
.df.pow()
function to calculate, element-wise, the power 4
of the created DataFrame
.DataFrame
on the console.DataFrame
and series
Let's say we have two objects, where one is a DataFrame
and another is a series
:
# importing pandas as pdimport pandas as pdimport numpy as np# Creating the DataFramedf = pd.DataFrame([[15, 3, None, 4],[11, None, 4, 3],[4, 0, 8, None],[5, 1, 2, 4]])# Creating Numpy arraydata = np.array([2, 1, 7, 9])# Converting to Python seriesseries = pd.Series(data)# Invoking floor division methodresult= df.pow(series)# printing resultsprint(result)
DataFrame
named df
, which consists of 16
numerical values that are distributed in a 4x4
frame.series
that consists of four integer values.df.pow(series)
function with series
as an argument.