I have this dataframe of financial data extracted with yfinance. I am trying to multiply the daily price of each stock by a vector of positions reflecting the quantity of stocks the portfolio is holding (57 EDP.LS stocks, 2 DIS stocks, none of the others). The code I am using is below, but all I get it a dataframe of NaNs the size of the original df + 4 rows indexed "0, 1, 2, 3" with more NaNs. What am I doing wrong?
from datetime import date
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
tickers = ['EDP.LS', 'DIS', 'GILD', 'FB'] #yahoo finance tickers
firstPositions = pd.Series([57, 2, 0, 0]) #order follows tickers list
today = date.today()
df = yf.download(tickers, start='2020-02-07', end=today)['Close']
firstValues = df.mul(firstPositions, axis=0)
Thank you in advance
If you want to multiply each column use axis=1, that should solve.
We should insert the ticker with the index of you mul serise
tickers = ['EDP.LS', 'DIS', 'GILD', 'FB'] #yahoo finance tickers
firstPositions = pd.Series([57, 2, 0, 0],index= tickers)
Then we can do multiple
firstValues = df.mul(firstPositions, axis=1)
tickers = ['EDP.LS', 'DIS', 'GILD', 'FB']
start_date = 'Your Start Date'
end_date = 'Your End Date'
exchange = 'yahoo'
column_name = 'Adj Close' # 'Open' 'High' 'Low' 'Close' 'Adj Close' 'Volume' #Your Columns
allData = pd.DataFrame()
finalTickers = []
for ticker in tickers:
try:
allData = pd.merge(allData, pd.DataFrame(pdr.get_data_yahoo(ticker, fields='price',
start=start_date, end=end_date)['Adj Close']),
right_index=True, left_index=True, how='outer')
# Appends tickers which have data
finalTickers.append(ticker)
except:
next
allData.columns = finalTickers
allData = allData.dropna(axis='columns')
allData
Then:
s = allData.mul(firstPositions_as_dataframe, axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.