猿问

尝试使用多行条件迭代pandas df

提前道歉。我对python很陌生。我创建了一个用日期时间戳索引的pandas df。我想根据特定列和下一行中的同一列遍历所有具有条件的行。根据下面的示例,我想根据绝对值(例如 3)检查“Streak”列,然后检查下一行以查看 Streak 列是否变为负值。


由于时间戳索引,我正在努力查看下一行。我已经尝试过.shift()无数次尝试使用loc和iloc以及Timedelta。他们对后者的问题是每行的时间戳之间没有一致的差异。


我的 df 看起来像


             Security      Difference     Buy/Sell     Streak  Price

Date 

2019-02-25       2330           500.0            1          1  238.0

2019-02-26       2330           400.0            1          2  239.0     

2019-02-27       2330           200.0            1          3  239.5

2019-03-05       2330          -600.0           -1         -1  233.0

2019-03-06       2330           190.0            1          1  234.0

我尝试过但失败的代码是


streaklength = 3

for index, row in mergeddf.iterrows():

    currentrow = mergeddf.index.get_loc(index)

    If (mergeddf.iloc[currentrow,’Streak’] >= streaklength & (mergeddf.iloc[Currentrow + 1, ‘Streak’]) == -1:

        Do something

编辑 - 轮流输出我想获得原始行的“价格”列和固定行数后的列的价格价格并返回该比率。


因此,对于固定行移动 n=2 的原始示例。我想返回一个变量 ouput1 (239.5) 和 ouput2 (234) 并返回 output1/output2 的比率


千巷猫影
浏览 158回答 1
1回答

一只名叫tom的猫

如果我正确理解了这个问题,那么您想要的是(df.Streak >= 3) & (df.Streak.shift(-1) < 0).稍微更改您自己示例中的第一行以确保满足这两个条件,这给出了以下内容:In [15]: dfOut[15]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Security&nbsp; Difference&nbsp; Buy/Sell&nbsp; Streak&nbsp; PriceDate2019-02-25&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;500.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 238.02019-02-26&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;400.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; 239.02019-02-27&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;200.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 239.52019-03-05&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; -600.0&nbsp; &nbsp; &nbsp; &nbsp; -1&nbsp; &nbsp; &nbsp; -1&nbsp; 233.02019-03-06&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;190.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; 234.0In [16]: (df.Streak >= 3) & (df.Streak.shift(-1) < 0)Out[16]:Date2019-02-25&nbsp; &nbsp; False2019-02-26&nbsp; &nbsp; False2019-02-27&nbsp; &nbsp; &nbsp;True2019-03-05&nbsp; &nbsp; False2019-03-06&nbsp; &nbsp; FalseName: Streak, dtype: bool要回答编辑过的问题,您可以使用此掩码获取相关输出:mask = (df.Streak >= 3) & (df.Streak.shift(-1) < 0)df['Output1'] = df[mask].Pricedf['Output2'] = df[mask].Price / df.shift(-2)[mask].Price结果如下:In [139]: dfOut[139]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Security&nbsp; Difference&nbsp; Buy/Sell&nbsp; Streak&nbsp; Price&nbsp; Output1&nbsp; &nbsp;Output2Date2019-02-25&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;500.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 238.0&nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN2019-02-26&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;400.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; 239.0&nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN2019-02-27&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;200.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 239.5&nbsp; &nbsp; 239.5&nbsp; 1.0235042019-03-05&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; -600.0&nbsp; &nbsp; &nbsp; &nbsp; -1&nbsp; &nbsp; &nbsp; -1&nbsp; 233.0&nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN2019-03-06&nbsp; &nbsp; &nbsp; 2330&nbsp; &nbsp; &nbsp; &nbsp;190.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; 234.0&nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN
随时随地看视频慕课网APP

相关分类

Python
我要回答