下一行匹配条件python pandas datafram

2024-04-19 00:49:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大约600万行的数据帧,我需要反复分析以进行模拟。下面是一个非常简单的数据表示。在

对于action=1的行

我正设法想出一个有效的办法来做这件事

For index,row in df.iterrows():

`Result = the first next row where (price2 is >= row.price1 +4) and index > row.index` 

或者如果不存在的话

return index+100(即活动超时)。在

^{pr2}$

结果是这样的:

       Action(y/n)  Price1  Price2  ExitRow(IndexOfRowWhereCriteriaMet)
0            0       14      2     9
1            1       8       1     8
2            0       3       1     102
3            0       1       5     103
4            1       7       3     7
5            0       3       1     105
6            1       8       2     8
7            0       2      11     107
8            0       3      12     108
9            0       1       1     109

我试过几种方法,都很慢。 这是最好的一个地图,但真的不够快。在

df['ExitRow'] = list(map(ATestFcn, df.index,df.price1))

def ATestFcn(dfIx, dfPrice1):
    ExitRow = df[((df.price2>(price1+4))&(df.index >dfIx)&    (df.index<=dfIx+TimeOut))].index.min()
    if pd.isnull(ExitRow):
        return dfIx+ TimeOut
    else:
        return ExitRow

我还用一个循环测试了这个,它大约慢了25%——但这是明智之举 基本相同。在

我认为必须有一个更聪明或更快的方法来实现这一点,掩码可能是有用的,除非你不能用这个数据填充,因为一行的price2可能是另一行的price2之后的数千行,而且我找不到一种方法来像TSQL中那样将合并转换为交叉应用。在


Tags: 数据方法dfindexreturntimeoutactionrow
1条回答
网友
1楼 · 发布于 2024-04-19 00:49:18

要找到符合条件的第一行的索引,可以使用

    cur_row_idx = 100 # we want the row after 100
    next_row_idx = (df[cur_row_idx:].Price2 >= df[cur_row_idx:].Price1 + 4).argmax()

然后,你想设置一个截止值,比如说,你得到的最大值是TimeOut-所以它可以是

^{pr2}$

我没有检查大型数据集的性能,但希望它能有所帮助。 如果需要,也可以将其包装成函数:

    def ATestFcn(dfIx, df, TimeOut):
        return np.min(((df[dfIx:].Price2 >= df[dfIx:].Price1 + 4).argmax() , dfIx + TimeOut))

编辑:刚测试过,速度相当快,见以下结果:

    df = pd.DataFrame()
    Price1 = np.random.randint(100, size=10e6)
    Price2 = np.random.randint(100, size=10e6)

    df["Price1"] = Price1
    df["Price2"] = Price2

    timeit ATestFcn(np.random.randint(1e6), df, 100)

    Out[62]: 1 loops, best of 3: 289 ms per loop

相关问题 更多 >