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

one
  • 2,205
  • 1
  • 15
  • 37
learning java
  • 23
  • 1
  • 3
  • Please consider accept my answer if you find it's possible. Or tell me is there something wrong. – one Aug 29 '19 at 11:09

2 Answers2

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]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2
index = reading_range.split(',')
eval('data.iloc[{}, {}]'.format(index[0], index[1]))

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

one
  • 2,205
  • 1
  • 15
  • 37
  • 1
    Maybe add new link - [link](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice/1832957#1832957) – jezrael Aug 29 '19 at 09:53