我试图从雅虎财经下载一个dataframe.为此,我使用下面的代码:`import yahoo_fin.stock_info as si import pandas pd
import yahoo_fin.stock_info as si
import pandas as pd
ticker_list = ["amzn", "aapl", "ba"]
historical_datas = {}
for ticker in ticker_list:
historical_datas[ticker] = si.get_data(ticker, start_date="12/04/2009", end_date="12/04/2019", index_as_date = True, interval="1mo")
historical_datas.to_csv("C:\Users\...")
但是我在输出中有一个错误
AttributeError Traceback (most recent call last)
Input In [5], in <cell line: 9>()
7 for ticker in ticker_list:
8 historical_datas[ticker] = si.get_data(ticker, start_date="12/04/2009", end_date="12/04/2019", index_as_date = True, interval="1mo")
----> 9 historical_datas.to_csv("C:\\Users\\...")
AttributeError: 'dict' object has no attribute 'to_csv'
我试着用代码做一个 Dataframe 来下载
df = pd.DataFrame(historical_datas)
df.to_csv("C:\\Users\\...")
但在这种情况下,我有一个错误If using all scalar values, you must pass an index
2条答案
按热度按时间lskq00tm1#
from_dict docs你可以使用
from_dict
,将dict转换为 Dataframefnvucqvd2#
这样试试
pd.DataFrame.from_dict(historical_datas)
然后你做
historical_datas.to_csv((“C:\Users...”,index = True)