如何过滤熊猫数据框中的数字?

时间:2020-05-23 23:22:09

标签: python pandas dataframe

我真的不知道该如何称呼,但是我的问题是,我正在做一个具有基本SMA交叉的交易算法。代码如下:

import pandas as pd
import pandas_datareader as data
import datetime as dt
import numpy as np
start = dt.datetime(2017, 1, 1)
end = dt.datetime(2020, 1, 20)
d = data.get_data_yahoo('URI', start, end) 

d['sma50'] = np.round(d['Close'].rolling(window=2).mean())
d['sma200'] = np.round(d['Close'].rolling(window=14).mean(), decimals = 2)
d['200-50'] = d['sma200'] - d['sma50']
_buy = -2
d['Crossover_Long'] = np.where(d['200-50'] < _buy, 1, 0)
d['buy'] = np.where(d['Crossover_Long']==1, 'buy', 'sell')
pd.set_option('display.max_rows', 400)
d.drop(['High', 'Low', 'Volume', 'Adj Close', 'Open'], axis=1, inplace=True)
d.dropna(inplace=True)
d.head()

所以前5行是:

    Close   sma50   sma200  200-50  Crossover_Long  buy
Date                        
2017-01-23  110.110001  111.0   109.04  -1.96   0   sell
2017-01-24  113.610001  112.0   109.35  -2.65   1   buy
2017-01-25  114.260002  114.0   109.67  -4.33   1   buy
2017-01-26  127.059998  121.0   110.87  -10.13  1   buy
2017-01-27  128.259995  128.0   112.22  -15.78  1   buy

如果有1,则应该买入;如果有0,则应该卖出。现在的问题是一种解决方法,即当sma2高于sma14时,而不是继续写sma2时,仅在出现交叉时才打印1。然后在中间设置0,直到下一个分频器为止。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在diff()系列上使用Crossover_Long方法-因为它已经被编码为0或1,所以当它改变状态时,一行将得到1或-1的差异只要。然后只需查找非0行并相应地进行标记即可。

d['Crossover_Long_Change']=d.Crossover_Long.diff()

# code it as 1 where the value is not 0 (i.e. there is a change)
d['Crossover_Long_Change']=d['Crossover_Long_Change'].fillna(0).map(lambda x: 1 if x!=0 else 0)
d.head()


    Close   sma50   sma200  200-50  Crossover_Long  buy     Crossover_Long_Change
Date                            
2017-01-23  110.110001  111.0   109.04  -1.96   0   sell    0
2017-01-24  113.610001  112.0   109.35  -2.65   1   buy     1
2017-01-25  114.260002  114.0   109.67  -4.33   1   buy     0
2017-01-26  127.059998  121.0   110.87  -10.13  1   buy     0
2017-01-27  128.259995  128.0   112.22  -15.78  1   buy     0