如何将这些yfinance数据转换为pandas数据框?

问题描述

我正在尝试将从yfinance获得的库存数据转换为数据框。它不会让我称呼日期或Adjusted close,所以我无法将其合并到我拥有的其他数据框中。这看起来很简单,但我真的很固执。希望您能提供帮助,谢谢!

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

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

输出

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

如您所见,“平价收市价”未在价格上方列出。我使用的是在后台创建的简单函数,可以让我更快地下载yfinance信息,称为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)

解决方法

解决方法:

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

考虑到df是水平放置的(我认为是),列的名称(是“ adj close”