Python Pandas for loop/while loop question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Python Pandas for loop/while loop question (/thread-25224.html) |
Python Pandas for loop/while loop question - mrashy - Mar-24-2020 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 RE: Python Pandas for loop/while loop question - deanhystad - Mar-24-2020 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. |