Search code examples
pythonpandaskeyerror

Pandas Key Error When Searching For Keyword In "Cell"


I am iterating over some data in a pandas dataframe searching for specific keywords, however the resulting regex search results in a KeyError: 19.

I've tried to pull out the data in the specific cell, place it in a string object and search through that, but every time I attempt to point anything to look a the data in that column, I get a KeyError: 19.

To preface my code example, I have pulled out specific chunks of the dataframe and placed them in a list of lists. (Of these chunks, I have kept all of the columns that were in the original dataframe)

Here is an example of the iteration I am attempting:

for eachGroup in mainList:
   for lineItem in eachGroup:
      if re.search(r'( keyword )', lineItem[19], re.I):
         dostuff

As you might have guessed, the data I am searching for keywords in is column 19 which has data formatted like this:

 3/23/2019 11:32:0 3/23/2019 11:32:0 3/23/2019 14:3:0 CSG CHG H6   27   1464D  Random Random Random  81

Every other attempt at searching for keywords in different columns executes fine without any errors. Why would this case alone return a KeyError?

To add some more clarity, even the following code produces the same KeyError:

for eachGroup in mainList:
   for lineItem in eachGroup:
      text = lineItem[19]

Solution

  • Here's a WTF moment...

    Instead of using python's smart for looping, I decided to be more granular and loop through with a while loop. Needless to say it worked.

    The below code implementation fixes the issue though why it does I have no clue:

    bigCount = len(mainList)
    count = 0
    while count < bigCount:
       while smallCount < len(mainList[count]):
          if re.search(r'( keyword )', mainList[count][smallCount][19], re.I):
             dostuff