【对比python】分组子集连续区间比上期 | 润乾 -欧洲杯在线开户
任务:计算各股票最长连涨天数
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) |
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_info = pd.read_csv(stock_file,sep="\t") |
15 | stock_info.sort_values(by='dt',inplace=true) |
16 | stock_group = stock_info.groupby(by='code') |
17 | max_rise_list = [] |
18 | for index,stock_g in stock_group: |
19 | code = stock_g.iloc[0]['code'] |
20 | max_rise_list.append([code,con_rise(stock_g)]) |
21 | max_rise_df = pd.dataframe(max_rise_list,columns=['code','con_rise']) |
22 | print(max_rise_df) |
集算器
a | b | ||
1 | e:\\txt\\stockrecords.txt | ||
2 | =file(a1).import@t() | ||
3 | =a2.sort(dt) | ||
4 | =a3.group(code) | ||
5 | =a4.new(code,func(a6,~):con_rise) | ||
6 | func | ||
7 | =(num=0,a6.max(num=if(cl>cl[-1],if(#==1,0,num 1),0))) |
单支股票的连续区间比上期思路:如果大于前一天的股价则加1,不大于则置0,最后查看序列中的最大值即可。把单支股票的计算方式写成函数,每支股票的表当做参数传入即可。集算器可以方便的在循环函数中调用函数得到结果。同样的思路,pandas的代码看起来就复杂的多。