3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数銘柄の米国株価をPython pandasで一度に取得する

Posted at

前回、銘柄名を指定して米国株価を取得しましたが、この方法では1つの銘柄しか取得できませんでした。

そこで今回は複数銘柄の株価一度に取得してみました。

#流れ
1.取得したい銘柄をCSVファイルに記載
2.CSVをjupyterフォルダに配置
3.銘柄毎にpandas datareaderで株価を取得

#詳細
1.取得したい銘柄をCSVファイルに記載
 ここではファイル名をtest_sp500.csvとします。

Symbol Name Sector
AAPL Apple Inc. Information Technology
AMZN Amazon.com Inc Consumer Discretionary
FB Facebook, Inc. Information Technology
NFLX Netflix Inc. Information Technology
MSFT Microsoft Corp. Information Technology
V Visa Inc. Information Technology

2.CSVをjupyterフォルダに配置
image.png

3.銘柄毎にpandas datareaderで株価を取得

import pandas as pd
import pandas_datareader.data as pdr
import datetime
from tqdm import tqdm

df = pd.read_csv('test_sp500.csv')

end = datetime.date.today()
start = end - datetime.timedelta(days=3)
stock = {} #株価を格納する辞書
error_symbols = [] #エラー用

for s in tqdm(df['Symbol']):
    try:
        stock[s] = pdr.DataReader(s,"yahoo",start, end)
    except:
        error_symbols.append(s)

取得できました。
image.png

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?