计算价格变化(在最近2分钟内)和价格范围(在2分钟之前)

时间:2018-11-23 10:48:23

标签: python python-3.x pandas dataframe list-comprehension

我有一个如下所述的数据框:

row_no,last_price,time
01,110.50,10:09
02,111.60,10:09
03,111.50,10:09
04,112.00,10:09
05,112.00,10:10
06,112.60,10:10
07,112.50,10:10
08,113.10,10:10
09,114.30,10:11
10,114.50,10:11
11,115.70,10:11
12,116.50,10:12
13,116.30,10:12
14,116.20,10:12
15,116.50,10:13
16,117.80,10:13
17,117.90,10:13
18,117.50,10:14
19,118.70,10:14
20,118.90,10:14
21,118.30,10:14
22,118.50,10:15
23,119.60,10:15
24,119.50,10:15
25,119.80,10:15

添加了“ Row_No”列以供理解。

最后一行的示例= 25:值应按以下方式计算:

  1. 需要在新列中计算“ Price_Change_in_last_2mins”,即 (第25行last_price-第15行last_price)* 100 /(第15行last_price)
  2. 需要在新列中计算“ Price_Range_before_2mins”(期间2分钟),即(从第05行到第14行的last_price的最大值)-(从第05行到第14行的last_price的最小值)* 100 /(从第05行到第14行的last_price)

我想要这样的结果:

row_no,last_price,time,Price_Change_in_last_2mins,Price_Range_before_2mins
01,110.50,10:09,NaN,NaN
02,111.60,10:09,NaN,NaN
03,111.50,10:09,NaN,NaN
04,112.00,10:09,NaN,NaN
05,112.00,10:10,NaN,NaN
06,112.60,10:10,NaN,NaN
07,112.50,10:10,NaN,NaN
08,113.10,10:10,NaN,NaN
09,114.30,10:11,NaN,NaN
10,114.50,10:11,NaN,NaN
11,115.70,10:11,NaN,NaN
12,116.50,10:12,NaN,NaN
13,116.30,10:12,NaN,NaN
14,116.20,10:12,NaN,NaN
15,116.50,10:13,NaN,NaN
16,117.80,10:13,NaN,NaN
17,117.90,10:13,,NaN,NaN
18,117.50,10:14,0.85,4.49
19,118.70,10:14,1.88,4.49
20,118.90,10:14,2.06,4.49
21,118.30,10:14,1.55,4.49
22,118.50,10:15,1.72,3.86
23,119.60,10:15,2.66,3.86
24,119.50,10:15,2.57,3.86
25,119.80,10:15,2.83,3.86

1 个答案:

答案 0 :(得分:1)

这是一种方法:

def last_2mins(x, df):
    from_time = x.name - datetime.timedelta(minutes=2)
    slice_2min = df.loc[from_time:x.name,:]
    first = slice_2min.iloc[0].last_price
    return (x.last_price - first)*100/first

def before_2mins(x, df):
    from_time = x.name - datetime.timedelta(minutes=2)
    slice_2min = df.loc[from_time:x.name,:]
    max_val = slice_2min.last_price.max()
    min_val = slice_2min.last_price.min()
    return (max_val - min_val)*100/min_val

df = df.set_index(df.time).drop(['time'], axis = 1)
indices = df.loc[df.index.max() - datetime.timedelta(minutes=1):].index.unique()

df_ = df.reset_index()
df_.loc[df_.time.isin(indices), 'last_2mins  '] = \
        df.loc[indices].apply(lambda x: last_2mins(x, df), axis = 1).values
df_.loc[df_.time.isin(indices), 'before_2mins'] = \
        df.loc[indices].apply(lambda x: before_2mins(x, df), axis = 1).values

        time               last_price  last_2mins  before_2mins
0  2018-11-23 10:09:00       110.5         NaN           NaN
1  2018-11-23 10:09:00       111.6         NaN           NaN
2  2018-11-23 10:09:00       111.5         NaN           NaN
3  2018-11-23 10:09:00       112.0         NaN           NaN
4  2018-11-23 10:10:00       112.0         NaN           NaN
5  2018-11-23 10:10:00       112.6         NaN           NaN
6  2018-11-23 10:10:00       112.5         NaN           NaN
7  2018-11-23 10:10:00       113.1         NaN           NaN
8  2018-11-23 10:11:00       114.3         NaN           NaN
9  2018-11-23 10:11:00       114.5         NaN           NaN
10 2018-11-23 10:11:00       115.7         NaN           NaN
11 2018-11-23 10:12:00       116.5         NaN           NaN
12 2018-11-23 10:12:00       116.3         NaN           NaN
13 2018-11-23 10:12:00       116.2         NaN           NaN
14 2018-11-23 10:13:00       116.5         NaN           NaN
15 2018-11-23 10:13:00       117.8         NaN           NaN
16 2018-11-23 10:13:00       117.9         NaN           NaN
17 2018-11-23 10:14:00       117.5    0.858369      2.323580
18 2018-11-23 10:14:00       118.7    1.888412      2.323580
19 2018-11-23 10:14:00       118.9    2.060086      2.323580
20 2018-11-23 10:14:00       118.3    1.545064      2.323580
21 2018-11-23 10:15:00       118.5    1.716738      2.832618
22 2018-11-23 10:15:00       119.6    2.660944      2.832618
23 2018-11-23 10:15:00       119.5    2.575107      2.832618
24 2018-11-23 10:15:00       119.8    2.832618      2.832618

稍后,我将专用于添加评论。如果您对Price_Range_before_2mins中的内容有把握,请告诉我。同时,希望这会有所帮助。