user3221876
user3221876

Reputation: 285

Python Pandas value from index position

This formula used to work for me. I tried to upgrade my Pandas version. It seems to work (although I got a boatload a strange message during the pip upgrade). But now I get a weird result from only this one type of operation.

postionVar = str(myDF[(myDF['TableFieldName']==myArray[innerLoop])].index)

I used to just get 0 now I get this instead:

"Int64Index([0], dtype='int64')"

This is literally what goes into my text string where I just need the index position value. So if 0 is the position of a value I don't get 0 anymore I get this: "Int64Index([0], dtype='int64')"

Upvotes: 1

Views: 377

Answers (1)

EdChum
EdChum

Reputation: 393893

The following would work:

postionVar = str(myDF[myDF['TableFieldName']==myArray[innerLoop]].index[0])

This accesses the first element of the index object returned and so can now be cast to the string representation as you desire

Example:

In [222]:

df = pd.DataFrame({'a': arange(5)})
str(df[df['a'] == 3].index[0])
Out[222]:
'3'

Upvotes: 2

Related Questions