Python Forum
Comparing Values Resulting from Function Outputs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Comparing Values Resulting from Function Outputs (/thread-20045.html)



Comparing Values Resulting from Function Outputs - firebird - Jul-25-2019

Hello Everyone!
I have some functions both pulling data, stocks for one and indices for the other, from yahoo finance. The output of the function looks something like this:

{'AAPL': 208.67, 'MSFT': 140.72}<--current price
{'AAPL': 207.67, 'MSFT': 138.9}<--open price
{'AAPL': 209.15, 'MSFT': 140.74}<--high
{'AAPL': 207.17, 'MSFT': 138.85}<--low
{'AAPL': -0.00081401155, 'MSFT': 0.010266409}<--percentage change
{'AAPL': 14991567, 'MSFT': 20738275}<--volume

What's attempted is to compare the percentage changes of elements of stocks = ['AAPL','MSFT','TSLA'] against that of indices = ['^IXIC','^DJI','^GSPC'] and then do something from there (such as print something for example).
Thank you for the help!
The error i get when running the script is:
Error:
Traceback (most recent call last): File "C:\Users\bgeor\Desktop\valChg_test1.py", line 38, in <module> if percent_stock < percent_index: File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops.py", line 2096, in f raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame objects
The script:
from yahoofinancials import YahooFinancials
#import all modules
from yahoofinancials import YahooFinancials
import pandas as pd1
import pandas as pd2
#Stock stickers to get data - declared globally
stocks = ['AAPL','MSFT','TSLA']
#NASDAQ Composite (^IXIC), Dow Jones Industrial Average (^DJI), S&P 500 (^GSPC)
indices = ['^IXIC','^DJI','^GSPC']


#Function to extract stock data
def getStockData():
            yahoo_financials = YahooFinancials(stocks)#stocks = ['AAPL','MSFT','TSLA']
            price = yahoo_financials.get_current_price()
            Open = yahoo_financials.get_open_price()
            High = yahoo_financials.get_daily_high()
            Low = yahoo_financials.get_daily_low()
            perChg1 = yahoo_financials.get_current_percent_change()
            return(price, Open, High, Low, perChg1)
#Function to extract index data
def getIndexData():
            yahoo_financials = YahooFinancials(indices)#indices = ['^IXIC','^DJI','^GSPC']
            price = yahoo_financials.get_current_price()
            Open = yahoo_financials.get_open_price()
            High = yahoo_financials.get_daily_high()
            Low = yahoo_financials.get_daily_low()
            perChg2 = yahoo_financials.get_current_percent_change()
            return(price, Open, High, Low, perChg2)
#Here we want to access the directional movement of stocks = ['AAPL','MSFT','TSLA']
#by comparing it to that of perccentage change of indices = ['^IXIC','^DJI','^GSPC']
for row in getStockData():
    percent_stock = pd1.DataFrame([{'perChg1' : row}])
    #carry on a logic here
    for val in getIndexData():
        percent_index = pd2.DataFrame([{'perChg2' : val}])
#compare percentage change of (stocks = ['AAPL','MSFT','TSLA']) against that of (indices = ['^IXIC','^DJI','^GSPC'])
        if percent_stock < percent_index:
            print(percent_stock)