如何将多个股票数据框保存到不同的单独 csv 文件中?
How to save multiple stocks data frame into different separate csv files?
示例代码:
tickers = ["FB", "AMZN", "AAPL", "NFLX", "GOOG", "^GSPC",...]
multpl_stocks = web.get_data_yahoo(tickers,
start = "2013-01-01",
end = "2014-03-01"())
我想将所有代码同时保存到不同的 csv 文件中。
当然,我知道下面的代码只是保存其中一个数据帧。
apple = web.get_data_yahoo("AAPL",
start = "2009-01-01",
end = datetime.now())
但是,我想将它们同时保存到一个 csv 文件中。
我不想一一做,因为这需要很多时间
我尝试了以下代码,但没有成功
apple, goog, fb = web.get_data_yahoo("AAPL","GOOG", "FB"
start = "2009-01-01",
end = "2013-01-01))
apple, goog, fb.to_csv(r("c:\desktop\users\fb.csv, goog.csv, apple.csv"))
我想要这样的结果并存储在我的电脑中:
fb.csv
goog.csv
apple.csv
你可以这样做:
import pandas as pd
from pandas_datareader import data as web #I assume this is what you have
from datetime import datetime
tickers = ["FB", "AMZN", "AAPL", "NFLX", "GOOG"] #and everything else
for stock in tickers:
stock_data = web.get_data_yahoo(stock, start = "2009-01-01", end = datetime.now())
df = pd.DataFrame(stock_data)
df.to_csv(stock + ".csv", index=False)
示例代码:
tickers = ["FB", "AMZN", "AAPL", "NFLX", "GOOG", "^GSPC",...]
multpl_stocks = web.get_data_yahoo(tickers,
start = "2013-01-01",
end = "2014-03-01"())
我想将所有代码同时保存到不同的 csv 文件中。
当然,我知道下面的代码只是保存其中一个数据帧。
apple = web.get_data_yahoo("AAPL",
start = "2009-01-01",
end = datetime.now())
但是,我想将它们同时保存到一个 csv 文件中。
我不想一一做,因为这需要很多时间
我尝试了以下代码,但没有成功
apple, goog, fb = web.get_data_yahoo("AAPL","GOOG", "FB"
start = "2009-01-01",
end = "2013-01-01))
apple, goog, fb.to_csv(r("c:\desktop\users\fb.csv, goog.csv, apple.csv"))
我想要这样的结果并存储在我的电脑中:
fb.csv
goog.csv
apple.csv
你可以这样做:
import pandas as pd
from pandas_datareader import data as web #I assume this is what you have
from datetime import datetime
tickers = ["FB", "AMZN", "AAPL", "NFLX", "GOOG"] #and everything else
for stock in tickers:
stock_data = web.get_data_yahoo(stock, start = "2009-01-01", end = datetime.now())
df = pd.DataFrame(stock_data)
df.to_csv(stock + ".csv", index=False)