Yuriy
Yuriy

Reputation: 11

Getting data about stocks using datareader

How do I make this look less ugly and fewer lines? Being able to get lots of stock exported into a csv file and also maybe being able to have a new line after each stocks information so I don't have to put it in myself. P.s. Also is there a way to get market cap and possibly float with it. please

This is what I'm trying to achieve


import datetime as dt

import matplotlib.pyplot as plt

from matplotlib import style

import pandas as pd

import pandas_datareader.data as web

style.use('ggplot')

start = dt.datetime(2019,12,20)

end = dt.datetime(2019,12,31)

df = web.DataReader('SDRL', 'yahoo', start, end)

df2 = web.DataReader('SLNO', 'yahoo', start, end)

df3 = web.DataReader('PTI', 'yahoo', start, end)

df4 = web.DataReader('LCTX', 'yahoo', start, end)

df5 = web.DataReader('CLPS', 'yahoo', start, end)

df6 = web.DataReader('AGTC', 'yahoo', start, end)

df7 = web.DataReader('NLNK', 'yahoo', start, end)

df8 = web.DataReader('SAVA', 'yahoo', start, end)

df9 = web.DataReader('MBOT', 'yahoo', start, end)

df10 = web.DataReader('HSDT', 'yahoo', start, end)

df11 = web.DataReader('CTXR', 'yahoo', start, end)

df12 = web.DataReader('ISCNF', 'yahoo', start, end)

df13 = web.DataReader('DCAR', 'yahoo', start, end)

df14 = web.DataReader('LAIX', 'yahoo', start, end)

df15 = web.DataReader('MRNS', 'yahoo', start, end)

df16 = web.DataReader('DRRX', 'yahoo', start, end)

df17 = web.DataReader('NLNK', 'yahoo', start, end)

df18 = web.DataReader('CANF', 'yahoo', start, end)

df19 = web.DataReader('CBKC', 'yahoo', start, end)

df20 = web.DataReader('GSAT', 'yahoo', start, end)

df21 = web.DataReader('CYDY', 'yahoo', start, end)

df22 = web.DataReader('SDRL', 'yahoo', start, end)

df23 = web.DataReader('MRSN', 'yahoo', start, end)

df24 = web.DataReader('ASRT', 'yahoo', start, end)

df25 = web.DataReader('BB', 'yahoo', start, end)

df26= web.DataReader('FCEL', 'yahoo', start, end)

df27 = web.DataReader('MDNAF', 'yahoo', start, end)

df28 = web.DataReader('INPX', 'yahoo', start, end)

df29 = web.DataReader('TKRFF', 'yahoo', start, end)

df30 = web.DataReader('PRTK', 'yahoo', start, end)





df.append(df2)
df52 = df.append(df2)

df52.append(df3)
df53 = df52.append(df3)

df53.append(df4)
df54 = df53.append(df4)

df54.append(df5)
df55 = df54.append(df5)

df55.append(df6)
df56 = df55.append(df6)

df56.append(df7)
df57 = df56.append(df7)

df57.append(df8)
df58 = df57.append(df8)

df58.append(df9)
df59 = df58.append(df9)

df59.append(df10)
df60 = df59.append(df10)

df60.append(df11)
df61 = df60.append(df11)

df61.append(df12)
df62 = df61.append(df12)

df62.append(df13)
df63 = df62.append(df13)

df63.append(df14)
df64 = df63.append(df14)

df64.append(df15)
df65 = df64.append(df15)

df65.append(df16)
df66 = df65.append(df16)

df66.append(df17)
df67 = df66.append(df17)

df67.append(df18)
df68 = df67.append(df18)

df68.append(df19)
df69 = df68.append(df19)

df69.append(df20)
df70 = df69.append(df20)

df70.append(df21)
df71 = df70.append(df21)

df71.append(df22)
df72 = df71.append(df22)

df72.append(df23)
df73 = df72.append(df23)

df73.append(df24)
df74 = df73.append(df24)

df74.append(df25)
df75 = df74.append(df25)

df75.append(df26)
df76 = df75.append(df26)

df76.append(df27)
df77 = df76.append(df27)

df77.append(df28)
df78 = df77.append(df28)

df78.append(df29)
df79 = df78.append(df29)

df79.append(df30)
df80 = df79.append(df30)







print(df80)

df80.to_csv('Gap-Ups.csv')
df80 = pd.read_csv('Gap-Ups.csv', parse_dates=True, index_col=0)

Upvotes: 1

Views: 986

Answers (2)

ansev
ansev

Reputation: 30920

Use pd.concat:

#Enter here all the list
companies_list = ['SDRL','SLNO','PTI']
df = pd.concat([web.DataReader(company, 'yahoo', start, end) 
                for company in companies_list])

Although in this way you will not distinguish the origin of the data, so I recommend concatenating with axis = 1 or using a dataframe dictionary.

#Enter here all the list
companies_list = ['SDRL','SLNO','PTI']
df = pd.concat([(web.DataReader(company, 'yahoo', start, end)
                    .add_suffix(f'_{company}'))
                for company in companies_list],axis = 1)

Creating a dict of DataFrames

d_company = {company:web.DataReader(company, 'yahoo', start, end)
             for company in companies_list}

Upvotes: 1

Randy Maldonado
Randy Maldonado

Reputation: 372

I suggest using the yfinance package. https://pypi.org/project/yfinance/

You can use

import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")

and it returns a single pandas dataframe with all of the stock data for whatever tickers you throw in. I believe it returns market cap as well, but not float data.

Upvotes: 1

Related Questions