Python Forum

Full Version: Python Pandas for loop/while loop question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I created the following dataframe:

df = pd.DataFrame([['a', 'car', 1], ['b', 'bus', 2], ['c', 'limo', 3]])
df.columns = ['letter', 'trans', 'num']

I am attempting to create a for or while loop to create a vector that returns the num column by recognizing the letter column (basically a vlookup) (also I know I can just do pd.num to get this vector but how I wrote this loop is kind of similar to something I'm doing at work and I am getting the same kind of errors but can't figure out why.)

The for loop is:

letter = ['a','b','c']
tr = []
for i in letter:
k = df[df.letter == str(i)]
k1 = k['num'][0]
tr.append(k1)

The while loop is:

letter = ['a', 'b', 'c']
tr = []
i = 0
while i <= len(df.num)-1:
k = df[df.letter == df.letter[i]]
k1 = k['num'][0]
tr.append(k1)
i = i + 1

In Juptyer notebook it is saying the error comes from k1 line and says a key error but I am confused when I repeat the code at put 'a' instead of df.letter[i] just to test one case, it works for each letter.

Can anyone explain why this isn't working or how to fix it?

Thanks
I think you are expecting k['num'] to return a list or closure. but it returns a DataFrame. The first time through the for loop the dataframe is [0 1] (what symbol does one use to denote a dataframe instead of a list or closure?) When you ask for k['num'][0] this works because the dataframe contains a zero. The second time through the loop k['num'] returns the dataframe [1 2]. This does not contain zero, so k['num'][0] raises a key error.