Pandas DataFrame | pow method
Start your free 7-days trial now!
Pandas DataFrame.pow(~) method computes the exponential power of the values in the source DataFrame and another scalar, sequence, Series or DataFrame, that is:
DataFrame ** other
Unless you use the parameters axis, level and fill_value, pow(~) is equivalent to computing the exponential power using the ** operator.
Parameters
1. otherlink | scalar or sequence or Series or DataFrame
The resulting DataFrame will be the exponential power of the source DataFrame and other.
2. axislink | int or string | optional
Whether to broadcast other for each column or row of the source DataFrame:
Axis | Description |
|---|---|
|
|
|
|
This is only relevant if the shape of the source DataFrame does not match that of other. By default, axis=1.
3. level | int or string | optional
The name or the integer index of the level to consider. This is relevant only if your DataFrame is Multi-index.
4. fill_valuelink | float or None | optional
The value to replace NaN before computing the exponential power. If the computation involves two NaN, then the result would still be NaN. By default, fill_value=None.
Return Value
A new DataFrame computed by the exponential power of the source DataFrame and other.
Examples
Basic usage
Consider the following DataFrames:
df = pd.DataFrame({"A":[3,4], "B":[5,6]})df_other = pd.DataFrame({"A":[1,1], "B":[2,2]})
A B | A B0 3 5 | 0 3 251 4 6 | 1 4 36
Computing the exponential power of df and df_other:
df.pow(df_other)
A B0 3 251 4 36
Here, we're computing the following element-wise exponential power:
3**1 5**24**1 6**2
Broadcasting
Consider the following DataFrame:
df = pd.DataFrame({"A":[3,4], "B":[5,6]})df
A B | A B0 3 5 | 0 3 251 4 6 | 1 4 36
Row-wise
By default, axis=1, which means that other will be broadcasted for each row in df:
df.pow([1,2]) # axis=1
A B0 3 251 4 36
Here, we're computing the following element-wise exponential power:
3**1 5**24**1 6**2
Column-wise
To broadcast other for each column in df, set axis=0 like so:
df.pow([1,2], axis=0)
A B0 3 51 16 36
Here, we're doing the following element-wise exponential power:
3**1 5**14**2 6**2
Specifying fill_value
Consider the following DataFrames:
df = pd.DataFrame({"A":[2,np.NaN], "B":[np.NaN,3]})df_other = pd.DataFrame({"A":[4,5], "B":[np.NaN,np.NaN]})
A B | A B0 2.0 NaN | 0 4 NaN1 NaN 3.0 | 1 5 NaN
By default, when we compute the power using pow(~), any operation with NaN results in NaN:
df.pow(df_other)
A B0 16.0 NaN1 NaN NaN
We can fill the NaN values before we compute the power by using the fill_value parameter:
df.pow(df_other, fill_value=1)
A B0 16.0 NaN1 1.0 3.0
Here, notice when the operation is between two NaN, the result is still NaN regardless of fill_value.