gpt4 book ai didi

python - 如何将迭代列表的新结果附加到数据框中的新列中

转载 作者:行者123 更新时间:2023-12-01 07:18:58 24 4
gpt4 key购买 nike

我尝试创建一个如下表,其中列表中的股票作为列附加到数据帧:

基本原理 CTRP EBAY ...... MPNGF

价格
股息
五年股息
市盈率
Hook 比率
预订价格市销率
账面值(value)
息税前利润
净收入
每股 yield 债务股本
三年平均返回

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

MPNGF 基础

价格
股息
五年股息
市盈率
Hook 比率
预订价格市销率
账面值(value)
息税前利润
净收入
每股 yield 债务股本
三年平均返回

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。

关于python - 如何将迭代列表的新结果附加到数据框中的新列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793244/

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