因此,我基本上已经有了此设置。我有一个包含以下内容的数据框:
> df.head()
> stock timestamp open high low adj_close volume
0 GERN 1532988000 3.34 3.59 3.59 3.59 4650479
1 GERN 1532901600 3.36 3.45 3.45 3.30 3571600
2 GERN 1532642400 3.44 3.50 3.50 3.34 2928697
3 GERN 1532556000 3.26 3.56 3.56 3.44 4209642
4 GERN 1532469600 3.37 3.42 3.42 3.22 3568900
5 GERN 1532383200 3.41 3.45 3.45 3.35 3323260
本质上,我想将开仓价调回1天和30天,并为此创建新的列,所以我想出了这小段代码,但是由于某种原因,也未附加new_df,为什么那?
import pandas as pd
df = pd.read_csv("financial_data_2000_2018_cleaned.csv", index_col=0)
# list of stucks that have all datapoints
stocks_list = ['GERN', 'CMCSA', 'AAPL', 'INTC', 'MSFT', 'QCOM', 'CSCO', 'NVDA',
'SIRI', 'HBAN', 'SBUX', 'ATVI', 'ADSK', 'CA', 'AMAT', 'ROST',
'HIBB', 'SYMC', 'EBAY', 'PZZA', 'GILD', 'MYL', 'ENDP', 'ADBE',
'ESRX', 'MRVL', 'CELG', 'BBBY', 'FNSR', 'URBN', 'FAST', 'AMZN']
new_df = pd.DataFrame()
for stock in stocks_list:
stock_df = df.loc[df['stock'] == stock]
stock_df_cp = stock_df.copy()
stock_df_cp['1_day_shift_open'] = stock_df_cp['open'].shift(1)
stock_df_cp['30_day_shift_open'] = stock_df_cp['open'].shift(30)
new_df.append(stock_df_cp)