【发布时间】:2023-02-04 04:49:04
【问题描述】:
enter image description here 我如何在 df 的另一个列中获得这个 2^ 值
我需要计算 2^ 值 有没有简单的方法可以做到这一点
| Value | 2^Value |
|---|---|
| 0 | 1 |
| 1 | 2 |
【问题讨论】:
标签: python pandas dataframe numeric calc
enter image description here 我如何在 df 的另一个列中获得这个 2^ 值
我需要计算 2^ 值 有没有简单的方法可以做到这一点
| Value | 2^Value |
|---|---|
| 0 | 1 |
| 1 | 2 |
【问题讨论】:
标签: python pandas dataframe numeric calc
您可以使用 numpy.power :
import numpy as np
df["2^Value"] = np.power(2, df["Value"])
的 输出 :
print(df)
Value 2^Value
0 0 1
1 1 2
2 3 8
3 4 16
【讨论】:
2 ** df["Value"]的原因?
您可以将 .apply 与 lambda 函数一起使用
df["new_column"] = df["Value"].apply(lambda x: x**2)
在 python 中,幂运算符是**
【讨论】: