anony123454323
anony123454323

Reputation: 71

how can I turn this yfinance data into a pandas dataframe?

I'm trying to turn this stock data I got from yfinance into a dataframe. It won't let me call the dates or Adjusted close so I can't merge it into other dataframes I have. This looks simple but i'm really stuck. Hope you can help, thanks!

price_history_i = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_a = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_c = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']

df = pd.DataFrame([price_history_i, price_history_a, price_history_c])
average_price = df.mean()
print(average_price)

Output:

Date
2019-12-31     9.863333
2020-01-02     9.903333
2020-01-03     9.883333
2020-01-06     9.883333
2020-01-07     9.883333
                ...    
2020-08-25    10.133333
2020-08-26    10.173333
2020-08-27    10.183333
2020-08-28    10.206667
2020-08-31    10.203334
Length: 169, dtype: float64

As you can see, the Adj Close isn't listed above the price. I'm using a simple function that I made in the backround that lets me download yfinance info quicker called get_price_history

import yfinance as yf



def get_price_history(ticker, sdate, edate):
    data = []
    data = yf.download(ticker, start=sdate, end=edate)
    return (data)

Upvotes: 1

Views: 6939

Answers (1)

aerijman
aerijman

Reputation: 2782

Workaround:

pd.DataFrame(df.mean(), columns = [df.T.columns[0]])

considering that df is laying horizontal (I think it is) and that the name of the columns (are "adj close")

Upvotes: 1

Related Questions