I have a question for you.

Asked 2 years ago, Updated 2 years ago, 106 views

I've been doing this and that for a week, but I couldn't get the results, so I'm inquiring.

I want to receive stock price data as Excel data at once as below, but I want to get various data at once by entering the list without typing the item several times through the for statement, how can I extract it easily?

**I would like to extract only revised closing price data for each item for a certain period of time and download it to Excel. Rather than entering codes for each event, it would be nice if it came out all at once ㅠ<

start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2020, 1, 27)


msft = web.DataReader("MSFT", "yahoo", start, end)
amzn = web.DataReader("AMZN", "yahoo", start, end)
googl = web.DataReader("GOOGL", "yahoo", start, end)
fb = web.DataReader("fb", "yahoo", start, end)
tencent = web.DataReader("0700.HK", "yahoo", start, end)
sdy = web.DataReader("SDY", "yahoo", start, end)
vnq = web.DataReader("VNQ", "yahoo", start, end)
sret = web.DataReader("SRET", "yahoo", start, end)
csi300 = web.DataReader("192090.KS", "yahoo", start, end)
vn30 = web.DataReader("245710.KS", "yahoo", start, end)

---

msft.rename(columns={"Adj Close":"msft_close"}, inplace = True)
amzn.rename(columns={"Adj Close":"amzn_close"}, inplace = True)
googl.rename(columns={"Adj Close":"googl_close"}, inplace = True)
fb.rename(columns={"Adj Close":"fb_close"}, inplace = True)
tencent.rename(columns={"Adj Close":"tencent_close"}, inplace = True)
sdy.rename(columns={"Adj Close":"sdy_close"}, inplace = True)
vnq.rename(columns={"Adj Close":"vnq_close"}, inplace = True)
sret.rename(columns={"Adj Close":"sret_close"}, inplace = True)
csi300.rename(columns={"Adj Close":"csi300_close"}, inplace = True)
vn30.rename(columns={"Adj Close":"vn30_close"}, inplace = True)

--

stock_price = pd.concat([msft['msft_close'], amzn['amzn_close'], 
                         googl['googl_close'], fb['fb_close'],
                         tencent['tencent_close'], sdy['sdy_close'], 
                         vnq['vnq_close'], sret['sret_close'], 
                         csi300['csi300_close'], vn30['vn30_close']
                         ], axis = 1)

for

2022-09-22 21:13

1 Answers

Among the repeated codes, we figure out "what is not repeated" and then extract them and collate them to create a proper object definition (where two properties are used) and once we've toured the array of objects, the code repeats the rest.

It's hard to say. It's not a big deal.

stock_items = [
    {
        'name': 'msft',
        'code': 'MSFT'
    },
    {
        'name': 'tencent',
        'code': '0700.HK'
    },
    # The following is omitted
]

for item in stock_items:
    foo = web.DataReader(item.code, 'yahoo', start, end)
    bar = item.name + '_close'
    foo.rename(columns = {'Adj Close': bar}, inplace = True)
    stock_price = pd.concat(foo[bar], axis = 1)


2022-09-22 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.