【发布时间】:2026-01-24 21:30:01
【问题描述】:
我正在尝试将从 yfinance 获得的股票数据转换为数据框。它不会让我调用日期或调整关闭,因此我无法将其合并到我拥有的其他数据帧中。这看起来很简单,但我真的卡住了。希望能帮到你,谢谢!
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)
输出:
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
如您所见,Adj Close 并未列在价格上方。我正在使用我在后台创建的一个简单函数,它可以让我更快地下载 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)
【问题讨论】:
-
“Adj Close 未在价格上方列出” ...因为您没有将那个意思列命名为任何东西,也许?您可以使用
iloc访问未命名的列 -
TypeError: mean() got an unexpected keyword argument 'columns'当我尝试添加列时 -
对...columns isn't a valid parameter。我的意思是rename
-
你能给我举个例子吗?
-
df.mean()返回一个系列。您可以重命名系列的部分 - pandas.pydata.org/pandas-docs/stable/reference/api/…
标签: python pandas dataframe yfinance