【对比Python】分组子集跨行计算并过滤分组结果

任务:找出发生过连续三交易日涨停(涨幅10%)的股票

Python

1 import pandas as pd
2 def con_rise(stock:pd.DataFrame):
3 ??? rise_day_list = []
4 ??? rise_num = 0
5 ??? shift_1 = stock['CL']/stock['CL'].shift(1)-1>=0.1
6 ??? for bl in shift_1:
7 ??????? if bl == False:
8 ??????????? rise_num = 0
9 ??????? else:
10 ??????????? rise_num+=1
11 ??????? rise_day_list.append(rise_num)
12 ??? return max(rise_day_list)
13 stock_file = 'E:\\txt\\StockRecords.txt'
14 stock = pd.read_csv(stock_file,sep='\t')
15 stock_g = stock.groupby(by = ['CODE'])
16 good_code = []
17 for index,group in stock_g:
18 ??? group = group.sort_values(by='DT')
19 ??? group = group.reset_index(drop = True)
20 ??? max_rise = con_rise(group)
21 ??? if max_rise>=5:
22 ??????? good_code.append(index)
23 print(good_code)

集算器

A
1 E:\\txt\\StockRecords.txt
2 =file(A1).import@t()
3 =A2.group(CODE).(~.sort(DT))
4 =A3.select(func(A5,~)>=5).(~.CODE)
5 func
6 =(rise=0,A5.(rise=if(CL/CL[-1]-1>=0.1,rise=if(!CL[-1],0,rise+1),0)))
7 =max(B6)

集算器一步一步计算非常明确,1.分组并排序;2.计算涨幅超过0.1的最大天数;3.过滤。自定义的函数同样可以放入循环函数中进行循环计算。

现金官网游戏下载