Search code examples
pythonpython-2.7pandasfor-loopquandl

For loop issues with Quandl -- Python


I'm trying to create a for-loop that automatically runs through my parsed list of NASDAQ stocks, and inserts their Quandl codes to then be retrieved from Quandl's database. essentially creating a large data set of stocks to perform data analysis on. My code "appears" right, but when I print the query it only prints 'GOOG/NASDAQ_Ticker' and nothing else. Any help and/or suggestions will be most appreciated.

import quandl
import pandas as pd
import matplotlib.pyplot as plt
import numpy

def nasdaq():
    nasdaq_list = pd.read_csv('C:\Users\NAME\Documents\DATASETS\NASDAQ.csv')
    nasdaq_list = nasdaq_list[[0]]
    print nasdaq_list

    for abbv in nasdaq_list:
        query = 'GOOG/NASDAQ_' + str(abbv)
        print query

    df = quandl.get(query, authtoken="authoken")
    print df.tail()[['Close', 'Volume']]

Solution

  • Iterating over a pd.DataFrame as you have done iterates by column. For example,

    >>> df = pd.DataFrame(np.arange(9).reshape((3,3)))
    >>> df 
       0  1  2
    0  0  1  2
    1  3  4  5
    2  6  7  8
    >>> for i in df[[0]]: print(i)
    0
    

    I would just get the first column as a Series with .ix,

    >>> for i in df.ix[:,0]: print(i)
    0
    3
    6
    

    Note that in general if you want to iterate by row over a DataFrame you're looking for iterrows().