0

My question is the inverse of this question: How to check if any value of a column is in a range (in between two values) in Pandas?

Instead of having a value in the column that I am trying to compare to a specific range, I actually have a range in the column and I'm trying to get all the rows from the DataFrame for a specific value.

data = {'Reward_Level':['0-4','0-4', '5-10', '5-10', '11-16'],
        'reward' :['1', '2', '3', '4', '10']}
df = pd.DataFrame(data)
value = 2 # want to return all the rows whose range in 'Reward_Level' covers this value
df = df["How do I do this part?"]
# df >> gives me the rows for ['0-4', '1'] and ['0-4', '2']

The way I know how to do it is to break out Reward_Level into two separate columns and compare against the minimum and then also compare against the maximum, but is there a way to do this without breaking these into new columns?

DoubleDouble
  • 328
  • 2
  • 12

1 Answers1

0

First create an auxiliary DataFrame with extracted limits of each range:

df2 = df.Reward_Level.str.extract(r'(?P<v1>\d+)-(?P<v2>\d+)').astype('int')

Because values will be used in numeric comparisons, they must be converted to int.

Then use just a boolean indexing:

df[df2.v1.le(value) & df2.v2.ge(value)]
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41