简体   繁体   中英

Python - have a function output multiple data frames

I would like to achieve the following.

  1. I would like the function to output a separate data frame for each stock for example
  2. I would like to save each data frame to a separate csv

the code doesnt work and i am unable to output the and save the separate csvs. Can you help me output separate dataframes and export the respective CSVS?

    def getdata(stock: str):
        # Company Quote Group of Items
        company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}?apikey=demo")
        company_quote = company_quote.json()
        share_price = float("{0:.2f}".format(company_quote[0]['price']))
        
        # Balance Sheet Group of Items    
        BS = requests.get(f"https://financialmodelingprep.com/api/v3/income-statement/{stock}?period=quarter&limit=400&apikey=demo")
        BS = BS.json()
        #Total Debt
        debt = float("{0:.2f}".format(float(BS[0]['totalDebt'])/10**9))
        #Total Cash
        cash = float("{0:.2f}".format(float(BS[0]['cashAndShortTermInvestments'])/10**9))
       
        # Income Statement Group of Items
        IS = requests.get(f"https://financialmodelingprep.com/api/v3/income-statement/{stock}?period=quarter&limit=400&apikey=demo")
        IS = IS.json()
        # Most Recent Quarterly Revenue
        qRev = float("{0:.2f}".format(float(IS[0]['revenue'])/10**9))
       
        # Company Profile Group of Items
        company_info = requests.get(f"https://financialmodelingprep.com/api/v3/profile/{stock}?apikey=demo")
        company_info = company_info.json()
        # Chief Executive Officer
        ceo = (company_info[0]['ceo'])
        return (share_price, cash, debt, qRev, ceo)
    
    stocks = ('AAPL')
    d = {}
    for stock in stocks:
        df[stock] = pd.DataFrame(getdata, columns=['Share Price','Total Cash', 'Total Debt', 'Q3 2019 Revenue', 'CEO'], index=tickers)
    print(d)

I'm not sure to understand your question. But if the fact is that you want a different DataFrame for each ticker, here is a solution.

Instead of:

for stock in stocks:
    df[stock] = pd.DataFrame(....

Try:

for stock in stocks:
    globals()['df_%s' %stock] = pd.DataFrame(...
    # And to save it, inside the loop
    globals()['df_%s' %stock].to_csv(stock+'.csv')

EDIT:

Thanx for the add. Here is the code

import joblib
from joblib import Parallel,delayed
import requests
import pandas as pd

def getdata(stock):
    
    # Company Quote Group of Items
    company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}?apikey=demo")
    company_quote = company_quote.json()
    share_price = float("{0:.2f}".format(company_quote[0]['price']))
    
    # Balance Sheet Group of Items    
    BS = requests.get(f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/{stock}?period=quarter&limit=400&apikey=demo")
    BS = BS.json()
    #Total Debt
    debt = float("{0:.2f}".format(float(BS[0]['totalDebt'])/10**9))
    #Total Cash
    cash = float("{0:.2f}".format(float(BS[0]['cashAndShortTermInvestments'])/10**9))
    
    # Income Statement Group of Items
    IS = requests.get(f"https://financialmodelingprep.com/api/v3/income-statement/{stock}?period=quarter&limit=400&apikey=demo")
    IS = IS.json()
    # Most Recent Quarterly Revenue
    qRev = float("{0:.2f}".format(float(IS[0]['revenue'])/10**9))
    
    # Company Profile Group of Items
    company_info = requests.get(f"https://financialmodelingprep.com/api/v3/profile/{stock}?apikey=demo")
    company_info = company_info.json()
    # Chief Executive Officer
    ceo = (company_info[0]['ceo'])

    globals()['df_%s' %stock] = pd.DataFrame({'symbol':[stock],'debt':[debt],'cash':[cash],'qRev':[qRev],'ceo':[ceo]})

    globals()['df_%s' %stock].to_csv(stock+'.csv')
    return(globals()['df_%s' %stock])

stocks = ['AAPL'] #, 'MSFT', 'GOOG', 'T', 'CSCO', 'INTC', 'ORCL', 'AMZN', 'FB', 'TSLA', 'NVDA']

number_of_cpu = joblib.cpu_count()
delayed_funcs = [delayed(getdata)(stock) for stock in stocks]
parallel_pool = Parallel(n_jobs=number_of_cpu,prefer="processes") # processes threads
globals()['df_%s' %stock] = parallel_pool(delayed_funcs)
df_AAPL

OUTPUT

在此处输入图像描述

在此处输入图像描述

It isn't necessary to return the DataFrame, as you save it in the function. But I did it to show you the possibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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