Mar-24-2020, 03:05 AM
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
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