python 从 Dataframe 下载数据

gzszwxb4  于 2023-04-19  发布在  Python
关注(0)|答案(2)|浏览(130)

我试图从雅虎财经下载一个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

lskq00tm

lskq00tm1#

from_dict docs你可以使用from_dict,将dict转换为 Dataframe

fnvucqvd

fnvucqvd2#

这样试试
pd.DataFrame.from_dict(historical_datas)
然后你做
historical_datas.to_csv((“C:\Users...”,index = True)

相关问题