【发布时间】:2017-10-28 23:25:49
【问题描述】:
我有一个数据框,价格 Df:
Close_x
2121.25
2119.25
2119.5
2115.25
2120
2118
2115.25
2116.25
2116.25
如果第一个 Close_x 值 (2121.25) 大于 Close_x 值向下 9 行 (2116.25) 我想要一个新列“利润”添加 100,如下所示:
Df['Profit'] = ''
for index, row in Df.iterrows():
if Df['Close_x'].shift(9) > Df['Close_x']:
Df['Profit'] == 100
else:
Df['Profit'] == -100
我也试过这个:
for index, row in Df.iterrows():
if Df['Close_x'] + 9 > Df['Close_x']:
Df['Profit'] == 100
else:
Df['Profit'] == -100
对于这两种尝试,我都收到以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
请注意,我在 Close_x 中有数千行,因此我需要根据“从当前值向下 9 行”之类的规则进行迭代,而不是调用诸如 [:9] 之类的特定切片。
【问题讨论】:
标签: python loops pandas dataframe iteration