根据索引位置对列值执行功能

时间:2019-05-22 11:32:56

标签: python pandas dataframe indexing

我有一个3列的数据框; ['close', 'BUY', 'SELL']'BUY''SELL'列是布尔值,表示在哪里买卖的指数位置-它们分散在很多行中-大约3000。我已经能够找到这些布尔值正确的指数( 1)使用:

returns[returns.BUY == 1]

我希望能够根据这些布尔位置找到第n个索引点,然后将一个函数应用于与索引点n和n + n对应的“关闭”列值。

谢谢!

Need a function that would only apply if the conditionals in column 'BUY' are true (1)

2 个答案:

答案 0 :(得分:0)

您可以使用

查找索引
returns[returns.BUY == 1].index.values.astype(int)

编辑: 不知道我是否理解正确。您可以尝试使用

构造+ n的索引
for i in range(1, len(df) - n):
    df.loc[i, 'index+n'] = df.loc[i+n, 'index']

然后您可以使用apply函数来使用您想使用的任何条件。

答案 1 :(得分:0)

您快到了,只需在列中添加列名并应用your_function

returns.loc[returns.BUY == 1, 'close'].apply(your_function)
相关问题