繁体   English   中英

如何将遍历列表的新结果附加到数据框中的新列中

[英]How may I append new results from iterating through a list, into a new column in the dataframe

我试图按如下方式创建一个表,其中列表中的股票作为列附加到数据框中:

基本面 CTRP EBAY ...... MPNGF

价钱
股利
五年红利
比率
挂钩比率
priceToBook price_to_sales
账面价值
息税前利润
净利
每股收益债务权益
三年平均回报

目前,根据下面的代码,只显示列表中的最后一个股票:

基础 MPNGF

价钱
股利
五年红利
比率
挂钩比率
priceToBook price_to_sales
账面价值
息税前利润
净利
每股收益债务权益
三年平均回报

from yahoofinancials import YahooFinancials
import pandas as pd
import lxml
from lxml import html
import requests
import numpy as np
from datetime import datetime


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')

    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

ecommerce = ['CTRP', 'EBAY', 'GRUB', 'BABA', 'JD', 'EXPE', 'AMZN', 'BKNG', 'MPNGF']

price=[]
dividend=[]
five_year_dividend=[]
pe_ratio=[]
pegRatio=[]
priceToBook=[]
price_to_sales=[]
book_value=[]
ebit=[]
net_income=[]
EPS=[]
DebtEquity=[]
threeYearAverageReturn=[]

for i, symbol in enumerate(ecommerce):     
    yahoo_financials = YahooFinancials(symbol)
    balance_sheet_url = 'https://finance.yahoo.com/quote/' + symbol + '/balance-sheet?p=' + symbol
    df_balance_sheet = scrape_table(balance_sheet_url)
    df_balance_sheet_de = pd.DataFrame(df_balance_sheet, columns = ["Total Liabilities", "Total stockholders' equity"])
    j= df_balance_sheet_de.loc[[1]]   
    j['DebtEquity'] = j["Total Liabilities"]/j["Total stockholders' equity"]
    k= j.iloc[0]['DebtEquity']

    X = yahoo_financials.get_key_statistics_data()
    for d in X.values():
        PEG = d['pegRatio']
        PB = d['priceToBook']
        three_year_ave_return = d['threeYearAverageReturn']

    data = [['price', yahoo_financials.get_current_price()], ['dividend', yahoo_financials.get_dividend_yield()], ['five_year_dividend', yahoo_financials.get_five_yr_avg_div_yield()], ['pe_ratio', yahoo_financials.get_pe_ratio()], ['pegRatio', PEG], ['priceToBook', PB], ['price_to_sales', yahoo_financials.get_price_to_sales()], ['book_value', yahoo_financials.get_book_value()], ['ebit', yahoo_financials.get_ebit()], ['net_income', yahoo_financials.get_net_income()], ['EPS', yahoo_financials.get_earnings_per_share()], ['DebtEquity', mee], ['threeYearAverageReturn', three_year_ave_return]]
    data.append(symbol.text)
    df = pd.DataFrame(data, columns = ['Fundamentals', symbol])
    df

请就上表中我可能出错的地方寻求您的建议? 非常感谢你!

您需要在 for 循环之外调用 df 。 您当前编写的代码将为每个循环重新创建一个新的 df。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM