Pandas DataFrame - 获取当前行下方的x行并进行比较

时间:2017-05-28 15:46:35

标签: python loops pandas dataframe iteration

我有一个数据框,价格为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),我想要一个新列,'Profit'添加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]。

2 个答案:

答案 0 :(得分:1)

您似乎需要numpy.where

N = 3
Df['Profit'] = np.where(Df['Close_x'].shift(3) > Df['Close_x'], 100, -100)  
Df.loc[Df.index < N,'Profit'] = np.nan      
print (Df)
   Close_x  Profit
0  2121.25     NaN
1  2119.25     NaN
2  2119.50     NaN
3  2115.25   100.0
4  2120.00  -100.0
5  2118.00   100.0
6  2115.25  -100.0
7  2116.25   100.0
8  2116.25   100.0

或者可能需要:

N = 3
for index,row in Df.iterrows():
        if index < N:
            continue
        if(Df.loc[index-N, 'Close_x'] > Df.loc[index, 'Close_x']):
            Df.loc[index, 'Profit'] = 100
        else:
            Df.loc[index, 'Profit'] = -100            
print (Df)
   Close_x  Profit
0  2121.25     NaN
1  2119.25     NaN
2  2119.50     NaN
3  2115.25   100.0
4  2120.00  -100.0
5  2118.00   100.0
6  2115.25  -100.0
7  2116.25   100.0
8  2116.25   100.0

答案 1 :(得分:0)

for index, row in Df.iterrows():
    if Df['Close_x'].shift(9) > Df['Close_x']:
        Df['Profit'] == 100
    else:
        Df['Profit'] == -100

您正在迭代数据框但是没有使用变量索引和行?这看起来不正确