0

There was a similar question long ago, but I'll re-open the topic as I'm getting a KeyError when trying to get the index.

What I have is basically a csv with sequential information :

1   0   0   2   3   0   0   4   0   5
0   0   0   0   0   1   0   2   3   0

each column is a different product (0 just means that it hasn't been bought yet) and what I want is to make sort of a linked list and arrange the bought products like this

1 -> 2 -> 3 -> 4 etc..

I need it like this because I need to perform operations on them in this particular order and so I need to get the index.

I know that I can do it with a nested loop and check each cell and create the linked list like this, but it's bugging me out that pandas gives me this error. As far as I see it's not deprecated and it should be working correctly. I haven't found anywhere someone mentioning KeyError from get_loc().

Here's my code:

 def get_current_prods(self,list):
      """This gets the sequence of only the products that are already available and makes a new list with the indexes"""
      counter= 1
      for i in range(0,self.num_of_fields):

        if counter in list.values:
            print list.index.get_loc(str(counter))
        counter=counter+1

if  __name__ == '__main__':

    b = bayes_inference()
    print b.probs
    print b.seq

    for row in range(0,b.probs.shape[0]):

        list = b.seq.ix[row]
        print list
        b.get_current_prods(list)

Here's the stack trace :

 File "/home/kavaev/bayes_inference.py", line 32, in <module>
    b.get_current_prods(list)
  File "/home/kavaev/bayes_inference.py", line 18, in get_current_prods
    print list.index.get_loc(str(counter))

  File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 2106, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc (pandas/index.c:4160)

  File "pandas/index.pyx", line 161, in pandas.index.IndexEngine.get_loc (pandas/index.c:4024)

  File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13161)

  File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13115)

KeyError: '1'
Community
  • 1
  • 1
  • I'm not sure you've five new enough information to answer the problem. What does each row represent? It's a good idea to post the output of what the correct dataframe would look like. – Ted Petrou Dec 06 '16 at 21:16

1 Answers1

1

Indexes are not, by default, strings. Does the code work if you make this change?

if counter in list.values:
    print list.index.get_loc(counter)

P.S. It's bad practice to overwrite built in functions like list when defining variables. Now you have no way it's more difficult to convert something to a list!

Alex
  • 12,078
  • 6
  • 64
  • 74
  • inadvertently, your answer helped me solve a different problem where i was receiving the same KeyError. I just needed to reset the index in my case, and it worked again. thanks! – truckbot Sep 15 '22 at 07:31