last5_ls = range(-5, 0, 1)
last25_ls = range(-25, 0, 1)
data = data.groupby('stockid')
data_last5 = data.nth(last5_ls)
data_last25 = data.nth(last25_ls)
close25_dict = {}
close5_dict = {}
stockid_ls = data_last25.index.drop_duplicates().tolist()
for sid in stockid_ls:
if len(data_last25.loc[sid, 'close']) >= 25:
close25_dict[sid] = data_last25.loc[sid, 'close'].tolist()
close5_dict[sid] = data_last5.loc[sid, 'close'].tolist()
stockid_ls = data_last5.index.drop_duplicates().tolist()
for sid in stockid_ls:
if len(data_last25.loc[sid, 'close']) >= 25:
close25_dict[sid] = data_last25.loc[sid, 'close'].tolist()
close5_dict[sid] = data_last5.loc[sid, 'close'].tolist()
data_last5.index.drop_duplicates().tolist()
和data_last25.index.drop_duplicates().tolist()
的结果相同。
但是,如果我将它们用作循环列表,前者要快得多。为什么会这样?
data_last25的示例:
close transtime
stockid
000001.SH 3145.7724 2017-02-07 11:25:00
000001.SH 3146.2715 2017-02-07 11:30:00
000001.SH 3145.4279 2017-02-07 13:05:00
000001.SH 3146.0700 2017-02-07 13:10:00
000001.SH 3145.5921 2017-02-07 13:15:00
000001.SH 3144.9807 2017-02-07 13:20:00
000001.SH 3141.3619 2017-02-07 13:25:00
000001.SH 3142.4082 2017-02-07 13:30:00
000001.SH 3141.7696 2017-02-07 13:35:00
000001.SH 3144.6115 2017-02-07 13:40:00
000001.SH 3144.9876 2017-02-07 13:45:00
000001.SH 3146.0163 2017-02-07 13:50:00
000001.SH 3145.1942 2017-02-07 13:55:00
000001.SH 3146.9230 2017-02-07 14:00:00
000001.SH 3147.7423 2017-02-07 14:05:00
000001.SH 3146.1607 2017-02-07 14:10:00
000001.SH 3145.6695 2017-02-07 14:15:00
000001.SH 3148.0311 2017-02-07 14:20:00
000001.SH 3150.0356 2017-02-07 14:25:00
000001.SH 3150.7101 2017-02-07 14:30:00
000001.SH 3149.4439 2017-02-07 14:35:00
000001.SH 3148.4014 2017-02-07 14:40:00
000001.SH 3147.6509 2017-02-07 14:45:00
000001.SH 3147.9084 2017-02-07 14:50:00
000001.SH 3150.6996 2017-02-07 14:55:00
000001.SZ 9.2900 2017-02-07 11:25:00
000001.SZ 9.2800 2017-02-07 11:30:00
000001.SZ 9.2800 2017-02-07 13:05:00
000001.SZ 9.2800 2017-02-07 13:10:00
000001.SZ 9.2800 2017-02-07 13:15:00
000001.SZ 9.2800 2017-02-07 13:20:00
000001.SZ 9.2800 2017-02-07 13:25:00
000001.SZ 9.2800 2017-02-07 13:30:00
000001.SZ 9.2800 2017-02-07 13:35:00
000001.SZ 9.2800 2017-02-07 13:40:00
000001.SZ 9.2800 2017-02-07 13:45:00
000001.SZ 9.2900 2017-02-07 13:50:00
000001.SZ 9.2800 2017-02-07 13:55:00
000001.SZ 9.2800 2017-02-07 14:00:00
000001.SZ 9.2800 2017-02-07 14:05:00
000001.SZ 9.2900 2017-02-07 14:10:00
000001.SZ 9.2900 2017-02-07 14:15:00
000001.SZ 9.2900 2017-02-07 14:20:00
000001.SZ 9.2900 2017-02-07 14:25:00
000001.SZ 9.3000 2017-02-07 14:30:00
000001.SZ 9.3000 2017-02-07 14:35:00
000001.SZ 9.2800 2017-02-07 14:40:00
000001.SZ 9.2900 2017-02-07 14:45:00
000001.SZ 9.2800 2017-02-07 14:50:00
000001.SZ 9.2900 2017-02-07 14:55:00
data_last5的示例:
close transtime
stockid
000001.SH 3149.4439 2017-02-07 14:35:00
000001.SH 3148.4014 2017-02-07 14:40:00
000001.SH 3147.6509 2017-02-07 14:45:00
000001.SH 3147.9084 2017-02-07 14:50:00
000001.SH 3150.6996 2017-02-07 14:55:00
000001.SZ 9.3000 2017-02-07 14:35:00
000001.SZ 9.2800 2017-02-07 14:40:00
000001.SZ 9.2900 2017-02-07 14:45:00
000001.SZ 9.2800 2017-02-07 14:50:00
000001.SZ 9.2900 2017-02-07 14:55:00
000002.SH 3297.8762 2017-02-07 14:35:00
000002.SH 3296.7849 2017-02-07 14:40:00
000002.SH 3295.9991 2017-02-07 14:45:00
000002.SH 3296.2701 2017-02-07 14:50:00
000002.SH 3299.2052 2017-02-07 14:55:00