选择Pandas中的行并将其放入列表的更快方法?

2024-06-06 19:58:58 发布

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

我有一个600万行的数据帧,目前我正在执行以下操作:

symbol = 'BPCL'
rows=[]
start = time.time()
times = fut_df.timestamp.tolist()
for t in times:
    s_df = fut_df[fut_df['timestamp'] == t]
    s_df.sort_values('expiry', inplace=True)
    if len(s_df) > 0:
        # s_df.reset_index(drop=True, inplace=True)
        # count += 1
        while len(s_df) < 3:
            s_df.loc[len(s_df)] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
        rows.append([symbol, t,
                     s_df['close_under'].iloc[0], 
                     s_df['contractname'].iloc[0], s_df['close'].iloc[0],
                     s_df['contractname'].iloc[1], s_df['close'].iloc[1], 
                     s_df['contractname'].iloc[2], s_df['close'].iloc[2])
print(time.time() - start)

这对我来说似乎不是很有效。而且要花很多时间。有没有更适合熊猫/Python的方法?我认为lambda可能工作得更快或者map

{}的示例:

        contractname           timestamp   close     bid     ask  close_under  bid_under  ask_under     expiry
7485094  BPCL20MAYFUT 2020-04-30 15:29:52  372.20  371.85  372.75       371.85     371.85      371.9 2020-05-01
7485095  BPCL20APRFUT 2020-04-30 15:29:52  371.50  371.45  373.65       371.85     371.85      371.9 2020-04-01
7485096  BPCL20APRFUT 2020-04-30 15:29:52  371.50  371.05  373.65       371.85     371.85      371.9 2020-04-01
7485097  BPCL20MAYFUT 2020-04-30 15:29:53  372.20  371.85  372.75       371.85     371.85      371.9 2020-05-01
7485098  BPCL20APRFUT 2020-04-30 15:29:53  371.50  371.05  373.65       371.85     371.85      371.9 2020-04-01
7485099  BPCL20MAYFUT 2020-04-30 15:29:54  372.20  372.00  372.70       371.00     371.00      371.9 2020-05-01
7485100  BPCL20APRFUT 2020-04-30 15:29:54  371.50  371.05  373.65       371.00     371.00      371.9 2020-04-01
7485101  BPCL20MAYFUT 2020-04-30 15:29:55  372.20  372.00  372.70       371.85     371.85      371.9 2020-05-01
7485102  BPCL20APRFUT 2020-04-30 15:29:55  371.50  371.35  373.65       371.85     371.85      371.9 2020-04-01
7485103  BPCL20JUNFUT 2020-04-30 15:29:56  372.45  372.15  373.95       371.90     371.85      371.9 2020-06-01
7485104  BPCL20APRFUT 2020-04-30 15:29:56  371.50  371.05  373.65       371.90     371.85      371.9 2020-04-01
7485105  BPCL20MAYFUT 2020-04-30 15:29:56  372.70  372.00  372.75       371.90     371.85      371.9 2020-05-01
7485106  BPCL20APRFUT 2020-04-30 15:29:57  371.50  371.10  373.65       371.85     371.85      371.9 2020-04-01
7485107  BPCL20MAYFUT 2020-04-30 15:29:57  372.70  372.00  372.75       371.85     371.85      371.9 2020-05-01
7485108  BPCL20APRFUT 2020-04-30 15:29:58  371.50  371.40  373.65       371.90     371.85      371.9 2020-04-01
7485109  BPCL20MAYFUT 2020-04-30 15:29:59  372.70  372.00  372.75       371.85     371.85      371.9 2020-05-01

生成行后的预期输出我将把它们放入数据帧中:

symbol timestamp close_under m1 m1_close m2 m2_close m3 m3_close

BPCL 2020-04-30 15:29:52 371.85 BPCL20APRFUT 371.50  BPCL20MAYFUT 372.20 0 0

当前代码大约需要28小时才能通过数据帧。我怎样才能加快速度?我使用multiprocessing/concurrent.futures一次浏览多个文件


Tags: 数据truedfcloselentimesymboltimestamp
1条回答
网友
1楼 · 发布于 2024-06-06 19:58:58

您可以尝试以下方法:

d = {'close_under':'first','close':'last','bid':'first'}
df = fut_df.groupby(['contractname', 'timestamp']).aggregate(d)

输出:

                        close_under  close     bid
contractname timestamp                            
2020-04-30   15:29:52        371.85  371.5  371.85
             15:29:53        371.85  371.5  371.85
             15:29:54        371.00  371.5  372.00
             15:29:55        371.85  371.5  372.00
             15:29:56        371.90  372.7  372.15
             15:29:57        371.85  372.7  371.10
             15:29:58        371.90  371.5  371.40
             15:29:59        371.85  372.7  372.00

相关问题 更多 >