Pandas DataFrame.rpow(~) 方法计算标量、序列、Series 或 DataFrame 以及源 DataFrame 中的值的指数幂,即:
other ** DataFrame
请注意,这与 pow(~) 方法相反,该方法执行以下操作:
DataFrame ** other
注意
除非您使用参数 axis 、 level 和 fill_value ,否则 rpow(~) 相当于使用 ** 运算符计算指数幂。
参数
1.other | scalar 或 sequence 或 Series 或 DataFrame
生成的 DataFrame 将是 other 和源 DataFrame 的指数幂。
2. axis | int 或 string | optional
是否为源DataFrame的每一列或每一行广播other:
|
轴 |
说明 |
|---|---|
|
|
|
|
|
|
仅当源 DataFrame 的形状与 other 的形状不匹配时,这才相关。默认情况下,axis=1 。
3. level | int 或 string | optional
要考虑的级别的名称或整数索引。仅当您的 DataFrame 是多索引时,这才相关。
4. fill_value | float 或 None | optional
在计算指数幂之前替换 NaN 的值。如果计算涉及两个 NaN ,那么其结果仍然是 NaN 。默认情况下,fill_value=None 。
返回值
通过源 DataFrame 和 other 的指数幂计算出的新 DataFrame 。
例子
基本用法
考虑以下数据帧:
df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df_other = pd.DataFrame({"A":[1,1], "B":[2,2]})
[df] | [df_other]
A B | A B
0 3 5 | 0 1 2
1 4 6 | 1 1 2
计算 df 和 df_other 的指数幂:
df.rpow(df_other)
A B
0 1 32
1 1 64
在这里,我们计算以下元素级指数幂:
1**3 2**5
1**4 2**6
广播
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df
A B
0 3 5
1 4 6
逐行
默认情况下, axis=1 ,这意味着 other 将为 df 中的每一行广播:
df.rpow([1,2]) # axis=1
A B
0 1 32
1 1 64
在这里,我们计算以下元素级指数幂:
1**3 2**5
1**4 2**6
按列
要为 df 中的每一列广播 other,请像这样设置 axis=0:
df.rpow([1,2], axis=0)
A B
a 1 1
b 16 64
在这里,我们计算以下元素级指数幂:
1**3 1**5
2**4 2**6
指定fill_value
考虑以下数据帧:
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 B
0 2.0 NaN | 0 4 NaN
1 NaN 3.0 | 1 5 NaN
默认情况下,当我们使用 rpow(~) 计算幂时,任何使用 NaN 的操作都会产生 NaN :
df.rpow(df_other)
A B
0 16.0 NaN
1 NaN NaN
在使用 fill_value 参数计算功效之前,我们可以填充 NaN 值:
df.rpow(df_other, fill_value=1)
A B
0 16.0 NaN
1 5.0 1.0
这里,请注意,当运算在两个 NaN 之间时,无论 fill_value 是什么,结果仍然是 NaN 。
相关用法
- Python Pandas DataFrame rank方法用法及代码示例
- Python Pandas DataFrame rdiv方法用法及代码示例
- Python Pandas DataFrame radd方法用法及代码示例
- Python PySpark DataFrame repartition方法用法及代码示例
- Python PySpark DataFrame replace方法用法及代码示例
- Python PySpark DataFrame rdd属性用法及代码示例
- Python Pandas DataFrame reset_index方法用法及代码示例
- Python Pandas DataFrame reorder_levels方法用法及代码示例
- Python Pandas DataFrame rsub方法用法及代码示例
- Python Pandas DataFrame round方法用法及代码示例
- Python Pandas DataFrame resample方法用法及代码示例
- Python Pandas DataFrame reindex方法用法及代码示例
- Python PySpark DataFrame randomSplit方法用法及代码示例
- Python Pandas DataFrame replace方法用法及代码示例
- Python Pandas DataFrame rolling方法用法及代码示例
- Python Pandas DataFrame rfloordiv方法用法及代码示例
- Python Pandas DataFrame rtruediv方法用法及代码示例
- Python Pandas DataFrame rename_axis方法用法及代码示例
- Python Pandas DataFrame rmod方法用法及代码示例
- Python Pandas DataFrame rmul方法用法及代码示例
- Python Pandas DataFrame rename方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | rpow method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
