I start using Python, I have a code which get information of stocks from Yahoo. I would like to store this data in a dataframe.
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())
This code works, now I would like to store this data in a dataframe.
#Create a dataframe to store the relevant data
df3 = pd.DataFrame(YahooFinancials(assets))
I want to store the data in pyhton, in a table, in order to analyse the data. The output from the print code is difficult to read. So the idea is not export the data to excel for now. The idea is to show the name of the assets, the pe ratio, the price to sales ratio,the currency, and the adjusted close price per day in a dataframe (table form). I don't think I can see the historical PE ratio or the price to sales ratio, which is not an issue. I receive now a error, ValueError: DataFrame constructor not properly called!
What am I doing wrong?
This section describes the steps to turn each data into a data frame. The return value of this library is in JSON format, so we need to convert it into a data frame.
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
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
You can also combine investment information into a single data frame. The rest is up to you.