我有多行多列的数据框来描述股票的价格。描述开盘价、收盘价等的列以及基于这些价格的其他公式。表示每分钟时间的行。“公式”列的前几行可能没有数据,因为在显示“公式”下的第一个值之前,它需要最少的行数
You may download a sample CSV here.
我有另一组代码,它只绘制最后一行并与前几行进行比较。这是一个简单的if/elif/else python,看起来很有效,如果我错了或者有更好的方法,请纠正我
if df.close[-1] >= 30:
cond17 = df.formula[-4:-1].max() <= 5
elif 15 <= df.close[-1] < 30:
cond17 = df.formula[-4:-1].max() <= 3
elif 5 <= df.close[-1] < 15:
cond17 = df.formula[-4:-1].max() <= 1
elif 2.5 <= df.close[-1] < 5:
cond17 = df.formula[-4:-1].max() <= 0.5
else:
cond17 = df.formula[-4:-1].max() <= 0.02
我在尝试获取这组条件以在新列“cond17”下输出值(True/False)时遇到问题。我搜索并阅读了np.where和np.select是要选择的选项,因为它们很快。但尝试了以下操作并得到了下面列出的相应错误
请告知我该如何申请这个案子
np.where-使用嵌套的where
df.loc[:, ('Mcond17')] = np.where(df.close >= 30, df.formula.shift(1).tail(3) <= 5,
(np.where(15 <= df.close < 30, df.formula.shift(1).tail(3) <= 3,
(np.where(5 <= df.close < 15, df.formula.shift(1).tail(3) <= 1,
(np.where(2.5 <= df.close < 5, df.formula.shift(1).tail(3) <= 0.5,
df.formula.shift(1).tail(3) <= 0.02)))))))
获取了以下错误:
(np.where(15 <= df.close < 30, df.tenkanspanbdist.shift(1).tail(3) <= 3,
File "C:\Users\***\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py", line 1326, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
np.选择-阅读如果超过2个条件,我应该使用这个
cond17list = [(df.close >= 30), (15 <= df.close < 30), (5 <= df.close < 15), (2.5 <= df.close < 5)]
cond17choices = [(df.formula[-4:-1].max() <= 5), (df.formula[-4:-1].max() <= 3), (df.formula[-4:-1].max() <= 1), (df.formula[-4:-1].max() <= 0.5)]
df.loc[:, ('cond17')] = np.select(cond17list, cond17choices, default=(df.formula[-4:-1].max() <= 0.02))
获取了以下错误:
cond17list = [(df.close >= 30), (15 <= df.close < 30), (5 <= df.close < 15), (2.5 <= df.close < 5)]
File "C:\Users\***\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py", line 1326, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
重新检查了我的代码,结果是一个简单的错误
我不应该使用.tail(),因为它只检查数据帧的最后几行
我需要检查每一行,因此应该使用.rolling()代替
相关问题 更多 >
编程相关推荐