简体   繁体   English

熊猫:附条件栏

[英]Pandas: appending column with condition

I have a sample time series data (stock) as below: 我有一个示例时间序列数据(股票),如下所示:

                 Date  PX_OPEN  PX_LAST
Date                                   
2011-01-03 2011-01-03    31.18    31.26
2011-01-04 2011-01-04    31.42    31.02
2011-01-05 2011-01-05    31.10    30.54
2011-01-06 2011-01-06    30.66    30.54
2011-01-07 2011-01-07    31.50    30.66
2011-01-10 2011-01-10    30.82    30.94

I would like to add a new column GAP based on the following conditions: 我想根据以下条件添加新的列GAP

  • If current day open is higher than previous day last, then GAP = up . 如果当天的开盘价高于前一天的开盘价,则GAP = up
  • If current day open is lower than previous day last, then GAP = down . 如果当天的开盘价比前一日的开盘价低,则GAP = down
  • Otherwise, GAP = unch . 否则, GAP = unch (Alternatively, up can be changed to +1, down to -1, and unch to 0.) (或者,可以将up更改为+1,将down更改为-1,然后将unch更改为0。)

I can do this with if and for loop, but that would defeat the efficiency of verctorized operation in Pandas . 我可以使用if和for循环来执行此操作,但这会降低Pandas中已验证操作的效率 Can anyone help? 有人可以帮忙吗?

Use nested np.where calls: 使用嵌套的np.where调用:

import numpy as np
df['GAP'] = np.where(df['PX_OPEN'] > df['PX_LAST'].shift(), 'up',
            np.where(df['PX_OPEN'] < df['PX_LAST'].shift(), 'down', 'unch'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM