0

Apologies for what feels like an incredibly basic problem, but I couldn't actually find any answers on this. I have a straightforward function to add items (stock symbols) to a list based on their last letter. To start off I'm using a list. 'nyse' is the dataframe. The function is this:

nysewarrants = ()

def warrants(x, y):
    for e in x:
        if x.Symbol[e][-1] == 'W':
            y.append(e)
    return y

warrants(nyse, nysewarrants)

The current error is, "KeyError: 'Symbol'". I tried "print (nyse.Symbol[9][-1])" and got the expected output.

Thanks very much for any help.

snapcrack
  • 1,422
  • 2
  • 14
  • 33
  • Sorry, data is linked to here in the response: http://stackoverflow.com/questions/25338608/download-all-stock-symbol-list-of-a-market – snapcrack Feb 21 '17 at 15:14

1 Answers1

1

Iterating over a pandas DataFrame iterates by column. Since you want to iterate by row, you need to use iterrows()

nysewarrants = []

def warrants(x, y):
    for index, row in x.iterrows():
        if x.Symbol[index][-1] == 'W':
            y.append(row)
    return y

warrants(nyse, nysewarrants)
Louise Davies
  • 10,919
  • 4
  • 33
  • 35