I'm new to python and I want to get the historical data of some stocks. I'm trying to use investpy, but it seems that it can only get one stock at a time.
Is this correct?
If so, how can I merge those single data into one dataframe?
I tried to run something like this, but got only one column (and without the company's name). yfinance doesn't work in my case.
import investpy as inv
stocks = ["WEGE3", "JHSF3"]
dfs = list()
for stock in stocks:
df = inv.get_stock_historical_data(stock=stock, country="Brazil", from_date="01/01/2020", to_date="01/01/2021")["Close"]
dfs.append(df)
import investpy as inv
import pandas as pd
stocks = ["WEGE3", "JHSF3"]
dfs = pd.DataFrame()
for stock in stocks:
df = inv.get_stock_historical_data(stock=stock, country="Brazil", from_date="01/01/2020", to_date="01/01/2021")["Close"]
dfs = dfs.append(df)
dfs = dfs.T
dfs.columns = stocks
dfs.head()
Related
I'm building a script to pull data from Yahoo finance on multiple different companies and have run into an issue in my loop:
import pandas as pd
import yfinance as yf
import datetime
import time
companies = ['AAPL', 'MSFT', 'AMZN', 'PYPL']
xlwriter = pd.ExcelWriter('marketcap.xlsx', engine='openpyxl')
company_metrics = {}
for company in companies:
company_metrics[company] = {}
company_info = yf.Ticker(company)
company_metrics[company]['Market Cap'] = company_info.info['marketCap']
df = pd.DataFrame.from_dict(company_metrics)
df.to_excel(xlwriter, sheet_name=company, index=False)
xlwriter.save()
The data pulls fine, but when continuing through the loop, each new company data gets added to the lasts until the last sheet is the new data including all prior data... I would like all my sheets to only include their own data on their own individual sheets.
Thank you for your time to read my post!
Try changing write line to:
df[company].to_excel(xlwriter, sheet_name=company, index=False)
beginner question coming up and cant seem to connect the dots.
I have a portfolio data frame called my_pf which includes the tickers that I use for collecting the opening price. I success in collecting the opening data via the next two steps.
#create a list from the column 'ticker'
my_tickers = my_pf['ticker'].tolist()
#collect the opening data per ticker
for ticker in my_tickers:
open_price = yf.Ticker(ticker).info.get('open')
print(ticker, open_price)
The next step is adding the extracted data to my initial data frame. But how would i go about this?
Thank you for your help in advance.
There are many ways to add data to a column, such as df.append() and pd.concat(), but we created our code with df.append(). We start with an empty data frame to create the stock column and the opening price column. Once we have the opening price, we add the brand name and opening price to the data frame we just created.
import pandas as pd
import yfinance as yf
# my_tickers = my_pf['ticker'].tolist()
my_tickers = ['msft', 'aapl', 'goog']
tickers = yf.Tickers(my_tickers)
df = pd.DataFrame(index=[], columns=['ticker','Open'])
for ticker in my_tickers:
open_price = yf.Ticker(ticker).info.get('open')
df = df.append(pd.Series([ticker,open_price], index=df.columns), ignore_index=True)
print(df)
ticker Open
0 msft 204.07
1 aapl 112.37
2 goog 1522.36
I am trying to read multiple csv stock price files all of which have following columns: Date,Time, Open, High, Low, Close. The code is:
import pandas as pd
tickers=['gmk','yandex','sberbank']
ohlc_intraday={}
ohlc_intraday['gmk']=pd.read_csv("gmk_15min.csv",parse_dates=["<DATE>"],dayfirst=True)
ohlc_intraday['yandex']=pd.read_csv("yndx_15min.csv",parse_dates=["<DATE>"],dayfirst=True)
ohlc_intraday['sberbank']=pd.read_csv("sber_15min.csv",parse_dates=["<DATE>"],dayfirst=True)
df=copy.deepcopy(ohlc_intraday)
for i in range(len(tickers)):
df[tickers[i]] = df[tickers[i]].iloc[:, 2:]
df[tickers[i]].columns = ['Date','Time',"Open", "High", "Low", "Adj Close", "Volume"]
df[tickers[i]]['Time']=[x+':00' for x in df['Time']]
However, I am then faced with the KeyError: 'Time'. Seems like columns are not keys.
Is it possible to read or convert it to a DataFrame format with keys being stock tickers (gmk, yandex, sberbank) and column names, so I can easily extract value using following code
ohlc_intraday['sberbank']['Date'][1]
What you could do is create a DataFrame that has a column that specifies the market.
import pandas as pd
markets = ["gmk", "yandex", "sberbank"]
markets = ["gmk_15min.csv", "yndx_15min.csv", "sberbank.csv"]
dfs = [pd.read_csv(market, parse_dates=["<DATE>"], dayfirst=True)
for market in markets]
# add market column to each df
for df in dfs:
df['market'] = market
# concatenate in one dataframe
df = pd.concat(dfs)
Then access what you want in this manner
df[df['market'] == 'yandex']['Date'].iloc[1]
I very new to Python and I want to replace an exact ticker with a reference to a column of a Data Frame I created from a CVS file, can this be done. i'm using:
import pandas as pd
import numpy as np
import pdblp as pdblp
import blpapi as blp
con = pdblp.BCon(debug=False, port=8194, timeout=5000)
con.start()
con.ref("CLF0CLH0 Comdty","PX_LAST")
tickers = pd.read_csv("Tick.csv")
so "tickers" has a colum 'ticker1' which is a list of tickers, i want to replace
con.ref("CLF0CLH0 Comdty","PX_LAST") with somthing like
con.ref([tickers('ticker1')],"PX_LAST")
any ideas?
assuming you would want to load all tickers into one df, i think it would look something like this:
df = pd.DataFrame(columns=["set your columns"])
for ticker in tickers.tickers1:
df_tmp = pd.DataFrame()
con.ref(ticker,"PX_LAST")
df_tmp = con.fetch #you'll have to fetch the records into a df
df.append(df_tmp)
Ended up using the following .tolist() function, and worked well.
tickers = pd.read_csv("Tick.csv")
tickers1=tickers['ticker'].tolist()
con.ref(tickers1,[PX_LAST])
I want to simply create a csv file from the constructed DataFrame so I do not have to use the internet to access the information. The rows are the lists in the code: 'CIK' 'Ticker' 'Company' 'Sector' 'Industry'
My current code is as follows:
def stockStat():
doc = pq('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
for heading in doc(".mw-headline:contains('S&P 500 Component Stocks')").parent("h2"):
rows = pq(heading).next("table tr")
cik = []
ticker = []
coName = []
sector = []
industry = []
for row in rows:
tds = pq(row).find("td")
cik.append(tds.eq(7).text())
ticker.append(tds.eq(0).text())
coName.append(tds.eq(1).text())
sector.append(tds.eq(3).text())
industry.append(tds.eq(4).text())
d = {'CIK':cik, 'Ticker' : ticker, 'Company':coName, 'Sector':sector, 'Industry':industry}
stockData = pd.DataFrame(d)
stockData = stockData.set_index('Ticker')
stockStat()
As EdChum already mentioned in the comments, creating a CSV out of a pandas DataFrame is done with the DataFrame.to_csv() method.
The dataframe.to_csv() method takes lots of arguments, they are all covered in the DataFrame.to_csv() method documentation. Here is a small example for you:
import pandas as pd
df = pd.DataFrame({'mycolumn': [1,2,3,4]})
df.to_csv('~/myfile.csv')
After this, the myfile.csv should be available in your home directory.
If you are using windows, saving the file to 'C:\myfile.csv' should work better as a proof of concept.