如何从数据框列中获取该列的值而不会出现错误

时间:2019-02-27 14:04:21

标签: python pandas finance

我正在尝试从一列中获取数据以用于同一列。插入数据可能有效,但是我不确定该怎么做。

import pandas as pd
df = pd.read_csv(r"C:\Users\drnov\Desktop\Stocks\TNA (3).csv")
del df['High']
del df['Low']
del df['Open']
del df['Volume']
del df['Adj Close']
df['Close'] = round(df['Close'] * 100) / 100

#Original formula (Unsimplified)
#=(AS4*(2/(12+1))+AT5*(1-(2/(12+1))))

a = df['EMA12'].shift(1) #cause of error
b = df['EMA26'].shift(1)

EMA12 = (df['Close'] * (2 /(12+1)) + 10 * (1 - (2/(12+1))))
df['EMA12'] = EMA12
df['EMA12'] = round(df['EMA12'] * 100) / 100
EMA26 = (df['Close'] * 2 / 27) + 10 * 1 - (2/27)
df['EMA26'] = EMA26
df['EMA26'] = round(df['EMA26'] * 100) /**strong text** 100
MACD = EMA12 - EMA26
df['MACD'] = MACD
df['MACD'] = round(df['MACD'] * 100) / 100
#df.head()
print(df)

实际:

AttributeError Traceback (most recent call last) <ipython-input-9-0690939d98cf> in <module> 
23 pd.stats.moments.ewma(d, com=period-1, adjust=False) 
24 return 100 - 100 / (1 + rs) ---> 
25 df['RSI'] = RSI(df['Close'], 14) 
26 df.tail() 
27 RSI(df, 14) <ipython-input-9-0690939d98cf> in RSI(series, period) 
20 d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses

21 d = d.drop(d.index[:(period-1)]) ---> 
22 rs = pd.stats.moments.ewma(u, com=period-1, adjust=False) 
23 pd.stats.moments.ewma(d, com=period-1, adjust=False) 
24 return 100 - 100 / (1 + rs) 
AttributeError: module 'pandas' has no attribute 'stats'

预期:

  

88 87 77 80 80 75 58 62 71 74 76 66 66 65 64 69 76 79 70 72 82 81 80   84 76 71 65 58 56 47 39 26 33 30 28 23 22 5 10 14 15 28 27 35 38 46 38   34 35 44 43 56 45 38 38 36 40 35 37 40 47 61 58 50 58 47 51 57 53 41   39 48 48 42 36 25 25 26 17 19 25 23 23 27 28 12 11 11 12 24 24 28 27   35 29 31 40 39 38 39 36 35 43 31 39 33 47 48 44 51 61 61 65 79 68 76   69 67 67 66 66 67 66 67 61 65 58 41 52 47 44 47

由于我将EMA12数据用作计算EMA12的变量,因此出现错误,我该如何绕过此错误或插入必要的数据。

0 个答案:

没有答案