【对比python】分组子集跨行计算并过滤分组结果 | 润乾 -pg游戏官网登录入口
任务:找出发生过连续三交易日涨停(涨幅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.过滤。自定义的函数同样可以放入循环函数中进行循环计算。