Return a value from a multi-col list based on single value being between 2 numbers on that list

Hi,

sorry don’t think title has explained that very well

I think this should be easy but cannot quite work it out, my code below does NOT not give me what I want but have included as some of the stuff I have tried

The data is a list in panda dataframe format

I basically have a single number &
I want to return the start time where this number is between the high & low numbers.

e.g. with 1560 I want to return = 04:00:00
2700 want to return = 05:00:00

print(dfst['low'].between(1005, 2005, inclusive = 'both'))
         comment   low   high starttime
0     FullCharge     0    501  03:00:00
1     HighCharge   500   1001  03:30:00
2      MedCharge  1000   2001  04:00:00
3      LowCharge  2000   2501  04:30:00
4      MinCharge  2500   3001  05:00:00
5  NominalCharge  3000  99999  05:25:00
0    False
1    False
2    False
3     True
4    False
5    False
Name: low, dtype: bool

Thanks I/A

That code tells you which rows meet the condition.

Use that as a subscript to get the rows themselves:

>>> dfst[dfst['low'].between(1005, 2005, inclusive = 'both')]
     comment   low  high starttime
3  LowCharge  2000  2501  04:30:00

Then to get the start time, specify the column and row:

>>> dfst[dfst['low'].between(1005, 2005, inclusive = 'both')]['starttime'].iloc[0]
'04:30:00'
1 Like

Hi,

thanks, I will have a look but think it might not work from the the off as the between is working on the wrong variable

in theory I need
[‘low’] < 1560 and [‘high’] > 1560 which I am assuming in not a thing…

not got time to test at the minute so perhaps I can make your advise work.

SOLVED

so simple & obvious perhaps but took ages to find such an example

x = 3200
res = dfst[(dfst[‘low’]<x) & (dfst[‘high’]>x)]
print (res)