gpt4 book ai didi

python - 合并财务数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:38 24 4
gpt4 key购买 nike

我试图弄清楚如何从雅虎财经获取财务信息(损益表、 Assets 负债表和现金流量)。我有一个名为符号的列表,其中包含所有股票代码(请参阅下面的代码)。最终我想要最终得到一个 csv,其中包含 4 年(2018 年、2017 年、2016 年、2015 年)连接的行。'''我可以手动执行此操作,但我想做的是自动执行此操作,以便我可以返回包含所有相关信息的 .csv 文件(77 列和 4*#ticker 符号行) enter image description here将上图变成: enter image description here

我已经弄清楚如何使用抓取工具从雅虎抓取数据。

from lxml import html
from lxml import html
import requests

import numpy as np

import pandas as pd
def scrape_table(url):
page = requests.get(url)
tree = html.fromstring(page.content)
table = tree.xpath('//table')
assert len(table) == 1

df = pd.read_html(lxml.etree.tostring(table[0], method='html'))[0]

df = df.set_index(0)
df = df.dropna()
df = df.transpose()
df = df.replace('-', '0')

# The first column should be a date
df[df.columns[0]] = pd.to_datetime(df[df.columns[0]])
cols = list(df.columns)
cols[0] = 'Date'
df = df.set_axis(cols, axis='columns', inplace=False)

numeric_columns = list(df.columns)[1::]
df[numeric_columns] = df[numeric_columns].astype(np.float64)

return df



def merge_IS_BS_CF(df_IS, df_BS, df_CF):
#merge the three financial statements - Income statement, balance sheet, cash flow into one dataframe
#return the dataframe
df_merge_IS_BS = pd.merge(df_IS, df_BS, on='Date')
df_merge_IS_BS_CF = pd.merge(df_merge_IS_BS, df_CF, on='Date')
return df_merge_IS_BS_CF

symbols = ['AAPL', 'MFT.NZ']

financials = {}
#create a dictionary of ticker names and their respective statements' urls
for symbol in symbols:
financials[symbol] = ['https://finance.yahoo.com/quote/' + symbol + '/financials?p=' + symbol, 'https://finance.yahoo.com/quote/' + symbol + '/balance-sheet?p=' + symbol, 'https://finance.yahoo.com/quote/' + symbol + '/cash-flow?p=' + symbol]
print (financials['AAPL'][0])
data = pd.DataFrame([])

我得到的结果是它没有将下一个股票行情数据连接到 pandas 数据框中。感谢您的帮助。

最佳答案

抱歉,这是我自己想出来的。对于下一个人,我的错误是没有意识到我必须保存附加的数据框。

symbols = ['AAPL', 'MFT.NZ']
financials = {}
#create a dictionary of ticker names and their respective statements' urls
for symbol in symbols:
financials[symbol] = ['https://finance.yahoo.com/quote/' + symbol + '/financials?p=' + symbol, 'https://finance.yahoo.com/quote/' + symbol + '/balance-sheet?p=' + symbol, 'https://finance.yahoo.com/quote/' + symbol + '/cash-flow?p=' + symbol]
print (financials['AAPL'][0])
data = pd.DataFrame()

for f in financials:
print (f)
df_income_statement = scrape_table(financials[f][0])
df_balance_sheet = scrape_table(financials[f][1])
df_cash_flow = scrape_table(financials[f][2])
oldmerge = merge_IS_BS_CF(df_income_statement, df_balance_sheet, df_cash_flow)
#print (oldmerge)
data = data.append(oldmerge)

关于python - 合并财务数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com