Reputation: 23
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
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
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