有条件地计算列的最大值和最小值

时间:2018-11-23 16:11:03

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

我有一个如下数据框:

row_no  stock_name  last_price  Var1
1       SAIL        501.00      0
2       SAIL        501.60      23
3       SAIL        500.00      0
4       SAIL        499.10      0
5       SAIL        499.40      0
6       SAIL        499.40      0
7       SAIL        502.00      0
8       SAIL        497.95      0
9       SAIL        495.55      20
10      SAIL        496.75      0
11      SAIL        496.75      0
12      SAIL        513.00      0
13      SAIL        497.00      0
14      SAIL        497.20      0
15      SAIL        497.00      0
16      SAIL        494.00      0
17      SAIL        497.00      0
18      SAIL        497.00      0
19      SAIL        497.00      0
20      SAIL        496.60      -9
21      SAIL        497.25      0

当Var1不为零时,需要计算最大和最小last_price。

当当前Var1不等于零时,将在Var1的先前非零值之间计算最大和最小last_price。

示例: 对于第9行:计算的最大last_price为Max(从第9行到第2行的last_price)= 502,最小的last_price作为Min(从第9行到第2行的last_price)= 495.55

我想要如下所示的最终数据框:

row_no  stock_name  last_price  Var1    Max_LTP Min_LTP
1       SAIL        501 0       0       0       0
2       SAIL        501.6       23      501.6   501
3       SAIL        500 0       0       0       0
4       SAIL        499.1       0       0       0
5       SAIL        499.4       0       0       0
6       SAIL        499.4       0       0       0
7       SAIL        502 0       0       0       0
8       SAIL        497.95      0       0       0
9       SAIL        495.55      20      502     495.55
10      SAIL        496.75      0       0       0
11      SAIL        496.75      0       0       0
12      SAIL        513 0       0       0       0
13      SAIL        497 0       0       0       0
14      SAIL        497.2       0       0       0
15      SAIL        497 0       0       0       0
16      SAIL        494 0       0       0       0
17      SAIL        497 0       0       0       0
18      SAIL        497 0       0       0       0
19      SAIL        497 0       0       0       0
20      SAIL        496.6       -9      513     494
21      SAIL        497.25      0   0   0       0

1 个答案:

答案 0 :(得分:3)

使用带有shiftcumsum的Var1创建组密钥,然后创建groupby,并将minmax的值分配回原始数据帧< / p>

mask=df.Var1.ne(0).shift().fillna(0).cumsum()
s=df.groupby(mask).last_price.agg(['min','max'])
s=s[df['Var1'].ne(0).groupby(mask).agg('any')]
s.index=df.index[df.Var1.ne(0)]



df[['Min_LTP','Max_LTP']]=s
df
Out[274]: 
    row_no stock_name  last_price  Var1  Max_LTP  Min_LTP
0        1       SAIL      501.00     0      NaN      NaN
1        2       SAIL      501.60    23    501.6   501.00
2        3       SAIL      500.00     0      NaN      NaN
3        4       SAIL      499.10     0      NaN      NaN
4        5       SAIL      499.40     0      NaN      NaN
5        6       SAIL      499.40     0      NaN      NaN
6        7       SAIL      502.00     0      NaN      NaN
7        8       SAIL      497.95     0      NaN      NaN
8        9       SAIL      495.55    20    502.0   495.55
9       10       SAIL      496.75     0      NaN      NaN
10      11       SAIL      496.75     0      NaN      NaN
11      12       SAIL      513.00     0      NaN      NaN
12      13       SAIL      497.00     0      NaN      NaN
13      14       SAIL      497.20     0      NaN      NaN
14      15       SAIL      497.00     0      NaN      NaN
15      16       SAIL      494.00     0      NaN      NaN
16      17       SAIL      497.00     0      NaN      NaN
17      18       SAIL      497.00     0      NaN      NaN
18      19       SAIL      497.00     0      NaN      NaN
19      20       SAIL      496.60    -9    513.0   494.00
20      21       SAIL      497.25     0      NaN      NaN