我有以下数据集:
df = pd.DataFrame({
'a': np.random.randn(6),
'b': np.random.randn(6),
})
我要创建另一列,
df['newCol] = 1/(1 + x) ^ df['a']
其中x = 0.15
有人可以告诉我该怎么做吗?
当我尝试运行以上代码以获取newCol
时,出现以下错误。
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
谢谢!
答案 0 :(得分:3)
您可以使用numpy.power
:
x = 0.15
df['newCol'] = np.power(1/(1 + x), df['a'])
或者就像评论中提到的@sammywemmy一样,使用**
来获得力量:
df['newCol'] = (1 / 1 + x) ** df.a
print (df)
a b newCol
0 0.026375 -0.711352 0.996321
1 0.140704 -0.428927 0.980527
2 0.889247 -0.316721 0.883130
3 0.270489 1.826090 0.962902
4 0.832062 -1.021876 0.890216
5 -0.314291 2.014869 1.044905