下面的代码用于创建下面的输出:

from yahooquery import Ticker

# Fetching data for AAPL
aapl = Ticker('AAPL')
types = ['asOfDate', 'TangibleBookValue', 'ShareIssued']
financial_data = aapl.get_financial_data(types, trailing=False)

# Dropping specific columns
columns_to_exclude = ['periodType', 'currencyCode']
financial_data.drop(columns=columns_to_exclude, inplace=True)
print(financial_data)

输出:

enter image description here

我想从history module中添加一个额外的列,它根据相应的asOfDate获取adjclose的价格. 所以输出应该是:

enter image description here

下面是获取价格历史数据的示例代码,其输出如下:

tickers = Ticker('aapl', asynchronous=True)

# Default period = ytd, interval = 1d
df = tickers.history(start='2019-01-01', end='2023-12-31')
df.head()

enter image description here

推荐答案

这是给你的:

financial_data = financial_data.reset_index()
df = df.reset_index()
df['date'] = pd.to_datetime(df['date'], format='Y-%m-%d')
financial_data = pd.merge(left=financial_data, right=df[['date','adjclose']], left_on='asOfDate', right_on='date', how='outer')
financial_data

  symbol   asOfDate   ShareIssued  TangibleBookValue       date    adjclose
0   AAPL 2020-09-30  1.697676e+10       6.533900e+10 2020-09-30  113.604172
1   AAPL 2021-09-30  1.642679e+10       6.309000e+10 2021-09-30  139.697617
2   AAPL 2022-09-30  1.594342e+10       5.067200e+10 2022-09-30  137.204224
3   AAPL 2023-09-30  1.555006e+10       6.214600e+10        NaT         NaN

Python相关问答推荐

如何将uint 16表示为float 16

如何使用函数正确索引收件箱?

在Python中,如何才能/应该使用decorator 来实现函数多态性?

CustomTKinter-向表单添加额外的输入字段

使用Curses for Python保存和恢复终端窗口内容

使用imap-tools时错误,其邮箱地址包含域名中的非默认字符

Polars -转换为PL后无法计算熵.列表

如何将我的位置与光强度数据匹配到折射图案曲线中?

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

比较两个二元组列表,NP.isin

输出中带有南的亚麻神经网络

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

在Python中管理打开对话框

如何过滤包含2个指定子字符串的收件箱列名?

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

OR—Tools CP SAT条件约束

Pandas计数符合某些条件的特定列的数量

在嵌套span下的span中擦除信息

在Python中调用变量(特别是Tkinter)

dask无groupby(ddf. agg([min,max])?''''