我正在尝试使用第一行股票价格来规范化我的数据框。以下是我的规范化功能。
def get_data(symbols, dates, addSPY=True, colname = 'Adj. Close'):
"""Read stock data (adjusted close) for given symbols from CSV files."""
##df = pd.DataFrame(index=dates)
for symbol in symbols:
save_place = get_data(symbol)
df = pd.read_csv(save_place, index_col='Date',
parse_dates=True, usecols=['Date', colname], na_values=['nan'])
df_temp = df_temp.rename(columns={colname: symbol})
df = df.join(df_temp)
return df
def normalize_data(df):
"""Normalize stock price using the first row of dataframe """
print(df)
return df/df[0,:]
Adj. Close
Date
2016-06-09 153.42
2016-06-08 154.0
2016-06-07 153.33
2016-06-06 152.73
2016-06-03 152.89
2016-06-02 153.5
2016-06-01 152.51
2016-05-31 153.74
2016-05-27 152.84
2016-05-26 152.44
2016-05-25 151.69
错误是:
File "util.py", line 40, in normalize_data
return df/df[0,:]
File "C:\Users\fange\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1992, in __getitem__
return self._getitem_column(key)
File "C:\Users\fange\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1999, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\fange\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1343, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type
答案 0 :(得分:1)
假设您开始使用pd.DataFrame
column
,'Adj.Close'
:
s = pd.to_numeric(df.squeeze()) # alternatively to df.squeeze(), you could use df.loc[:, 'Adj. Close']
Adj. Close
Date
2016-06-09 153.42
2016-06-08 154.00
2016-06-07 153.33
2016-06-06 152.73
2016-06-03 152.89
2016-06-02 153.50
2016-06-01 152.51
2016-05-31 153.74
2016-05-27 152.84
2016-05-26 152.44
2016-05-25 151.69
使用
s.div(s.iloc[0])
的产率:
Adj. Close
Date
2016-06-09 1.000000
2016-06-08 1.003780
2016-06-07 0.999413
2016-06-06 0.995503
2016-06-03 0.996545
2016-06-02 1.000521
2016-06-01 0.994069
2016-05-31 1.002086
2016-05-27 0.996220
2016-05-26 0.993612
2016-05-25 0.988724