当我在雅虎金融中导入多只股票时,我会得到一个多指数。我正在尝试导出为 csv 文件,并且它们导入具有相同索引的相同数据集。我尝试使用index = False
in.to_csv
方法,但它删除了行索引,这是我不想要的。
基本上我找不到stocks_sample_csv
与stocks_sample
.
import pandas as pd
from datetime import date
import yfinance as yf
list_symbols = ["CERS", "CERU", "CETV", "CEVA", "CFA", "CFBK",
"CFFI", "CFFN", "CFGE", "CFNB", "CFNL", "CFO",
"CFRX", "CFRXW", "CFRXZ", "CG", "CGEN", "CGIX",
"CGNX", "CGO", "CHCI", "CHCO", "CHDN", "CHEF",
"CHEV", "CHFC", "CHFN", "CHI", "CHKE", "CHKP",
"CHLN", "CHMG", "CHNR", "CHOP", "CHRS", "CHRW",
"CHSCM", "CHSCN", "CHSCO", "CHSCP"]
start = date(2017, 10, 1)
end = date(2020, 6, 25)
stocks_sample = yf.download(list_symbols, start, end, group_by='ticker')
#for checking
print(stocks_sample)
stocks_sample.columns.levels[0]
stocks_sample.columns.levels[1]
stocks_sample.to_csv('stocks_sample.csv')
stocks_sample_csv = pd.read_csv('stocks_sample.csv')
#now check the imported .csv file
print(stocks_sample_csv)
stocks_sample_csv.columns.levels[0]
stocks_sample_csv.columns.levels[1]
顺便说一句,我正在这样做,所以我不需要每次打开我的 python 控制台时都下载。
任何帮助将不胜感激。