首页 > 解决方案 > 在 Python 中将打印结果存储在数据框中

问题描述

我开始使用 Python,我有一个从 Yahoo 获取股票信息的代码。我想将此数据存储在数据框中。

yahoo_financials = YahooFinancials(assets)

print(yahoo_financials.get_historical_price_data("2013-01-01", "2021-01-30", "daily"))
print(yahoo_financials.get_currency())
print(yahoo_financials.get_pe_ratio())
print(yahoo_financials.get_price_to_sales())

此代码有效,现在我想将此数据存储在数据框中。

#Create a dataframe to store the relevant data
df3 = pd.DataFrame(YahooFinancials(assets))

我想将数据存储在 pyhton 中的表中,以便分析数据。打印代码的输出很难阅读。所以这个想法不是现在将数据导出到excel。这个想法是在数据框(表格)中显示资产的名称、市盈率、市销率、货币和调整后的收盘价。我认为我看不到历史市盈率或市销率,这不是问题。我现在收到一个错误,ValueError:DataFrame 构造函数没有正确调用!

我究竟做错了什么?

标签: pandasdataframeyahoo-finance

解决方案


本节介绍将每个数据转换为数据框的步骤。这个库的返回值是JSON格式的,所以我们需要把它转换成一个数据框。

  1. 转换价格数据(FB)
import pandas as pd
from yahoofinancials import YahooFinancials
assets = ["FB", "AMZN", "AAPL", "NFLX", "GOOG"]

yahoo_financials = YahooFinancials(assets)

daily = yahoo_financials.get_historical_price_data("2021-01-01", "2021-01-30", "daily")

df = pd.read_json(str(daily).replace("'", '"'), orient='records')

    FB  AMZN    AAPL    NFLX    GOOG
eventsData  {}  {}  {}  {}  {}
firstTradeDate  {'formatted_date': '2012-05-18', 'date': 13373...   {'formatted_date': '1997-05-15', 'date': 86370...   {'formatted_date': '1980-12-12', 'date': 34547...   {'formatted_date': '2002-05-23', 'date': 10221...   {'formatted_date': '2004-08-19', 'date': 10929...
currency    USD     USD     USD     USD     USD
instrumentType  EQUITY  EQUITY  EQUITY  EQUITY  EQUITY
timeZone    {'gmtOffset': -18000}   {'gmtOffset': -18000}   {'gmtOffset': -18000}   {'gmtOffset': -18000}   {'gmtOffset': -18000}
prices  [{'date': 1609770600, 'high': 275.0, 'low': 26...   [{'date': 1609770600, 'high': 3272.0, 'low': 3...   [{'date': 1609770600, 'high': 133.610000610351...   [{'date': 1609770600, 'high': 540.799987792968...   [{'date': 1609770600, 'high': 1760.65002441406...

fb_df = pd.DataFrame(df.loc['prices', 'FB'])

fb_df.head()
    date    high    low     open    close   volume  adjclose    formatted_date
0   1609770600  275.000000  265.200012  274.779999  268.940002  15106100    268.940002  2021-01-04
1   1609857000  272.399994  268.209991  268.290009  270.970001  9871600     270.970001  2021-01-05
2   1609943400  267.750000  260.010010  262.000000  263.309998  24354100    263.309998  2021-01-06
3   1610029800  271.609985  264.779999  265.899994  268.739990  15789800    268.739990  2021-01-07
4   1610116200  268.950012  263.179993  268.309998  267.570007  18512500    267.570007  2021-01-08
  1. 转换投资信息
per_ratio = yahoo_financials.get_pe_ratio()
per_ratio.update({'idx':'ratio'})
ratio = pd.DataFrame(per_ratio, index=[0])

currency = yahoo_financials.get_currency()
currency.update({'idx':'currency'})
curr = pd.DataFrame(currency, index=[1])

volume = yahoo_financials.get_price_to_sales()
volume.update({'idx':'volume'})
vol =pd.DataFrame(volume, index=[2])

ratio
    FB  AMZN    AAPL    NFLX    GOOG    idx
0   25.602575   93.74306    35.79062    87.56415    35.471867   ratio

currency
    FB  AMZN    AAPL    NFLX    GOOG    idx
1   USD     USD     USD     USD     USD     currency

volume
    FB  AMZN    AAPL    NFLX    GOOG    idx
2   8.55742     4.623472    7.531772    9.433204    7.214964    volume

您还可以将投资信息合并到一个数据框中。剩下的就看你了。


推荐阅读