简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM