2

I'm quite new with python, however, I have to accomplish some assignment and I am struggling now on a problem. I try to get the index of the element in a table A when some other parameter from this table A corresponds to a value in a list B. The table A also already contains a column "index" where all elements are numerated from 0 till the end. Moreover, the values in tableA.parameter1 and listB can coincide only once, multiple matches are not possible. So to derive the necessary index I use a line

t=tableA.index[tableA.parameter1==listB[numberObservation]]

However, what I get as a result is something like:

t Int64Index([2], dtype='int64')

If I use the variable t in this format Int64Index, it doesn't suit for the further code I have to work with. Actually, I need only 2 as an integer number, without all this redundant rest.

Can somebody please help me to circumvent my problem? I am in total despair and would be grateful for any help.

2 Answers 2

6

Try .tolist()

t=tableA.index[tableA.parameter1==listB[numberObservation]].tolist()

This should return

t = [2] 

a list "without all the redundant rest" :)

Sign up to request clarification or add additional context in comments.

2 Comments

hi! I have tested your solution as well, thanks for your help. Especially it is useful if I have more than one match. Then t as a list should provide a good solution. However, I still have one problem. When I run your code, I get the error message 'Invalid frame count (= tableA.index[tableA.parameter1==listB])'. Moreover, when I debug the code and run only the command in IPython console without assigning the result to the variable - it works... Do you have any idea where the problem could be?
I haven't faced the error 'Invalid frame count' with pandas. Is it possible for you to post a complete test program, with only the important details?
1

What package is giving you Int64Index? This looks vaguely numpy-ish, but numpy arrays define __index__ so a single element array of integer values will seamlessly operate as indices for sequence lookup.

Regardless, assuming t is supposed to be exactly one value, and it's a sequence type itself, you can just do:

t, = tableA.index[tableA.parameter1==listB[numberObservation]]

That trailing comma changes the line from straight assignment to iterable unpacking; it expects the right hand side to produce an iterable with exactly one value, and that one value is unpacked into t. If the iterable has 0 or 2+ values, you'll get a ValueError.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.