Pandas学习笔记,如何用列的值过滤行

通过tushare引入DataFrame

d = ts.get_hist_data('600848', start='2015-01-05', end='2015-01-09')
data = pd.DataFrame(d)
print(data)
print(data.ma5 > data.ma20)
             open   high  close    low    volume  price_change  p_change  \
date                                                                       
2015-01-09  11.68  11.71  11.23  11.19  44851.56         -0.44     -3.77   
2015-01-08  11.70  11.92  11.67  11.64  56845.71         -0.25     -2.10   
2015-01-07  11.58  11.99  11.92  11.48  86681.38          0.31      2.67   
2015-01-06  11.13  11.66  11.61  11.03  59199.93          0.35      3.11   
2015-01-05  11.16  11.39  11.26  10.89  46383.57          0.14      1.26   

               ma5    ma10    ma20     v_ma5    v_ma10     v_ma20  turnover  
date                                                                         
2015-01-09  11.538  11.363  11.682  58792.43  60665.93  107924.27      1.54  
2015-01-08  11.516  11.349  11.647  57268.99  61376.00  105823.50      1.95  
2015-01-07  11.366  11.251  11.543  55049.74  61628.07  103010.58      2.97  
2015-01-06  11.182  11.155  11.382  54854.38  63401.05   98686.98      2.03  
2015-01-05  11.156  11.212  11.370  58648.75  68429.87  100765.24      1.59  
date
2015-01-09    False
2015-01-08    False
2015-01-07    False
2015-01-06    False
2015-01-05    False
dtype: bool
data.ma5 > data.ma20返回的是一列布尔值,如果使用data[A],A是一个布尔值的Series,那么对应True的行会返回,False的行会被过滤,例如:
print(data[data.ma5 > 11.3])
             open   high  close    low    volume  price_change  p_change  \
date                                                                       
2015-01-09  11.68  11.71  11.23  11.19  44851.56         -0.44     -3.77   
2015-01-08  11.70  11.92  11.67  11.64  56845.71         -0.25     -2.10   
2015-01-07  11.58  11.99  11.92  11.48  86681.38          0.31      2.67   

               ma5    ma10    ma20     v_ma5    v_ma10     v_ma20  turnover  
date                                                                         
2015-01-09  11.538  11.363  11.682  58792.43  60665.93  107924.27      1.54  
2015-01-08  11.516  11.349  11.647  57268.99  61376.00  105823.50      1.95  
2015-01-07  11.366  11.251  11.543  55049.74  61628.07  103010.58      2.97  
可以看到ma5 < 11.3的行被过滤了。
posted @ 2017-09-05 21:33  imageSet  阅读(542)  评论(0编辑  收藏  举报