【问题标题】:Merging Financial Data合并财务数据
【发布时间】:2019-05-20 08:18:26
【问题描述】:

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

我已经想出了如何使用刮刀从雅虎刮取数据。

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([])

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

【问题讨论】:

  • 这是不可读的。您不能指望人们在没有数据的情况下浏览您的代码并为您“调试”它。最好的方法是 1. 从示例数据框开始 2. 解释您尝试实现的目标。 3. 也包括数据框形式的预期输出。 4. 解释现在什么不工作。
  • 感谢您的反馈并修复了我在显示代码时遇到的错误。我添加了一些图片,希望它现在更有意义。

标签: python pandas


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多