learning java
learning java

Reputation: 23

Cannot index by location index with a non-integer key

I am very new to python.my code is below -

reading_range = "0:54, 2:34" #this is my variable 
data.iloc[reading_range]  #this line is giving error ,because the value is coming as '0:54, 2:34'

but I want something like

data.iloc[0:54, 2:34]

Thanks in advance

Upvotes: 2

Views: 9625

Answers (2)

one
one

Reputation: 2585

index = reading_range.split(',')
eval('data.iloc[{}, {}]'.format(index[0], index[1]))

see what eval() does: What does Python's eval() do?

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

The colon and comma are syntactical sugar, for a tuple and slice(..) objects. You can generate something equivalent like:

reading_range = slice(0,54), slice(2,34)
data.iloc[reading_range]

Upvotes: 3

Related Questions