3

I'm using the Quandl database service API and its python support to download stock financial data.

Right now, I'm using the free SFO database which downloads year operational financial data.

For example, this query code passes the last 6-8 years of data for stock "CRM" to the dataframe.

df=quandl.get('SF0/CRM_REVENUE_MRY')

df
Out[29]: 
                   Value
Date                    
2010-01-31  1.305583e+09
2011-01-31  1.657139e+09
2012-01-31  2.266539e+09
2013-01-31  3.050195e+09
2014-01-31  4.071003e+09
2015-01-31  5.373586e+09
2016-01-31  6.667216e+09

What I want to do with this is to recursively pass it a list of about 50 stocks and also grab 6-8 other columns from this database using different query codes appended on to the SFO/CRM_ part of the query.

qcolumns = ['REVUSD_MRY', 
            'GP_MRY', 
            'INVCAP_MRY', 
            'DEBT_MRY', 
            'NETINC_MRY', 
            'RETEARN_MRY', 
            'SHARESWADIL_MRY', 
            'SHARESWA_MRY',  
            'COR_MRY', 
            'FCF_MRY', 
            'DEBTUSD_MRY', 
            'EBITDAUSD_MRY', 
            'SGNA_MRY', 
            'NCFO_MRY', 
            'RND_MRY']

So, I think I need to:

a) run the query for each column and in each case append to the dataframe. b) Add column names to the dataframe. c) Create a dataframe for each stock (should this be a panel or a list of dataframes? (apologies as I'm new to Pandas and dataframes and am on my learning curve.
d) write to CSV

could you suggest or point me?

This code works to do two queries (two columns of data, both date indexed), renames the columns, and then concatenates them.

df=quandl.get('SF0/CRM_REVENUE_MRY')
df = df.rename(columns={'Value': 'REVENUE_MRY'})
dfnext=quandl.get('SF0/CRM_NETINC_MRY')
dfnext = dfnext.rename(columns={'Value': 'CRM_NETINC_MRY'})
frames = [df, dfnext]
dfcombine = pd.concat([df, dfnext], axis=1)     # now question is how to add stock tag "CRM" to frame

dfcombine
Out[39]: 
             REVENUE_MRY  CRM_NETINC_MRY
Date                                    
2010-01-31  1.305583e+09      80719000.0
2011-01-31  1.657139e+09      64474000.0
2012-01-31  2.266539e+09     -11572000.0
2013-01-31  3.050195e+09    -270445000.0
2014-01-31  4.071003e+09    -232175000.0
2015-01-31  5.373586e+09    -262688000.0
2016-01-31  6.667216e+09     -47426000.0

I can add recursion to this to get all the columns (there are around 15) but how do I tag each frame for each stock? Use a key? Use a 3D panel? Thanks for helping a struggling python programmer!

leeprevost
  • 384
  • 1
  • 3
  • 15

0 Answers0