Pandas DataFrame.pow
Pandas DataFrame.pow
The DataFrame.pow method in pandas is used to perform element-wise exponentiation (power) of DataFrame values with another DataFrame, Series, or scalar value. This method is flexible and allows alignment on specified axes and levels.
Syntax
The syntax for DataFrame.pow is:
DataFrame.pow(other, axis='columns', level=None, fill_value=None)Here, DataFrame refers to the pandas DataFrame on which the operation is performed.
Parameters
| Parameter | Description |
|---|---|
other | A scalar, DataFrame, or Series to raise the values of the DataFrame to. |
axis | Defines the axis along which to align the other. Default is 'columns'. |
level | Broadcast across a level, matching other if the axis is a MultiIndex. |
fill_value | Value to use to fill missing entries in the DataFrame or other. Default is None. |
Returns
A DataFrame with the result of the power operation.
Examples
Raising a DataFrame to a Scalar Power
Raise all elements of a DataFrame to the power of a scalar value.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
# Raise each element to the power of 2
print("DataFrame raised to the power of 2:")
df_pow = df.pow(2)
print(df_pow)Output
DataFrame raised to the power of 2:
A B C
0 1 16 49
1 4 25 64
2 9 36 81Element-Wise Power Using Another DataFrame
Perform element-wise exponentiation with another DataFrame.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
data2 = {
'A': [2, 2, 2],
'B': [3, 3, 3],
'C': [1, 1, 1]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform element-wise power operation
print("Element-wise power operation:")
df_result = df1.pow(df2)
print(df_result)Output
Element-wise power operation:
A B C
0 1 64 7
1 4 125 8
2 9 216 9Using the fill_value Parameter
Use fill_value to handle missing values during the power operation.
Python Program
import pandas as pd
# Create two DataFrames with missing values
data1 = {
'A': [1, None, 3],
'B': [4, 5, None],
'C': [7, 8, 9]
}
data2 = {
'A': [2, 2, 2],
'B': [3, 3, 3],
'C': [1, 1, 1]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform power operation with fill_value
print("Power operation with fill_value:")
df_result = df1.pow(df2, fill_value=1)
print(df_result)Output
Power operation with fill_value:
A B C
0 1 64 7.0
1 1 125 8.0
2 9 1 9.0Applying Power Along a Specific Axis
Use the axis parameter to specify the alignment of the operation.
Python Program
import pandas as pd
# Create a DataFrame and a Series
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
series = pd.Series([2, 3, 1], index=['A', 'B', 'C'])
# Perform power operation along columns
print("Power operation along columns:")
df_result = df.pow(series, axis='columns')
print(df_result)Output
Power operation along columns:
A B C
0 1 64 7.0
1 4 125 8.0
2 9 216 9.0Summary
In this tutorial, we explored the DataFrame.pow method in pandas. Key takeaways include:
- Performing scalar and element-wise exponentiation using
pow. - Handling missing values with the
fill_valueparameter. - Aligning operations along specific axes.
The DataFrame.pow method is a powerful and flexible way to perform element-wise exponentiation in pandas.