How to use the pandas DataFrame.pow() function in Python
Overview
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.
Syntax
The syntax of the function is given below:
DataFrame.pow(other, axis='columns', level=None, fill_value=None)
Parameter values
The DataFrame.pow() function takes the following argument values:
other: This can beDataFrame, sequence, series, or scalar.axis:defaut='columns', this can be1,'columns',0, or'index'.level:default=None, this broadcasts value across a level and can either beintorNone.fill_value:default=None, this can either be afloatvalue orNone, and it helps to fill empty (NaN) fields.
Return value
This method returns a DataFrame after performing certain
Explanation
We can use this function in multiple ways. Some of the scenarios where we use this function are listed below:
- When both are
DataFrames - When there is a
DataFrameand a constant - When there is a
DataFrameand series
Both are DataFrames
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)
- Lines 4–7: We create a
DataFramenameddf1, which has four rows and four columns. - Lines 9–12: We create a
DataFramenameddf2, which has the same dimensions asdf1. - Line 14: We invoke the
DataFrame.pow()function from the pandas module to calculate, element-wise, the exponential power between theDataFramesthat we created. - Line 16: We display the results on the console.
DataFrame and constant
Let'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)
- Lines 4-7: We create a
DataFramenameddf. - Line 9: We invoke the
df.pow()function to calculate, element-wise, the power4of the createdDataFrame. - Line 11: We print the resultant
DataFrameon 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)
- Lines 5–8: We create a
DataFramenameddf, which consists of16numerical values that are distributed in a4x4frame. - Line 12: We create a pandas's
seriesthat consists of four integer values. - Line 14: We invoke the
df.pow(series)function withseriesas an argument. - Line 16: We print the results to the console.