2

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

1
  • Please consider accept my answer if you find it's possible. Or tell me is there something wrong. Commented Aug 29, 2019 at 11:09

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

Comments

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

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

1 Comment

Maybe add new link - link

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.