我有一个函数,我将迭代,以找到一个浮动数字的第一个实例,然后打印‘我们击中利润’。在下面的例子中,我想从WAP列中找到8.49并停止查找。我得到错误消息:在profit_Price: TypeError:'numpy.float64‘对象中的x不可迭代
profit_Price = round((Wap_price * 0.020) + Wap_price,2)
def profit_stop(x):
for x in profit_Price:
if x == 8.49:
print('we hit profit')
else:
return x
#calls the above function 'profit_stop'
df['WAP'].apply(profit_stop)发布于 2017-04-14 08:23:23
如果您的目的是比较,则不应该担心停止(除非您的DataFrame非常大)。pandas依赖于numpy比较,这种比较非常有效,运行速度将比Pythonfor循环快。
运行df['WAP'] == 8.49或np.close(df['WAP'], 8.49)将为series中的每个元素返回一个布尔数组(True/False)。您可以使用它筛选您的series以获得想要的值:
df[np.close(df['WAP'], 8.49)]这将返回只包含WAP为8.49行的DataFrame。
发布于 2017-04-14 08:15:20
wap = df['WAP']
wap = [float(x) for x in wap] # lets make sure type is correct
for x in wap:
while x < 8.49:
print(x)
break
else:
print('we hit it')
break在接近8.49时,这种情况应该停止。
https://stackoverflow.com/questions/43407107
复制相似问题