0

I want to have a table like this

a   b   c
0   16  .2
16  18  .4  
18  20  1   
20  22  1   
22  24  1   
24  26  1   
26  28  1   
28  30  1   
30  32  1   
32  34  1   
34  36  1   
36  38  1   
38  99  1   

I then want to input a value and if it's in between column a and b's value, then return column c's value.

I thought of making a dataframe with range values and seeing if the value in is the range, but I also need to check float values:

x = [
    range(0,16),
    range(16,18),
    range(18,20),
    range(22,24),
    range(24,26),
    range(26,28),
    range(28,30),
    range(30,32),
    range(32,34),
    range(34,36),
    range(36,38),
    range(38,99),
    ]

values = [.2, .4, 1,1,1,1,1,1,1,1,1,1]

df_c = pd.DataFrame([x, values]).transpose()

Is there a better way to do this?

1
  • What do you want to output if val is not between a and b? Commented Jun 29, 2017 at 17:59

3 Answers 3

1
def c_if_between(a, b, c, val):
    interval = sorted(a,b)
    if interval[0] < val < interval[1]:
        return c
Sign up to request clarification or add additional context in comments.

Comments

0
def c_if_between(a, b, c, val):
    if sorted([a, b, val])[1] == val:
        return c

Comments

0
import numpy as np
np.array(c)[(value >= np.array(a)) & (value < np.array(b))]

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.