0

I have a dataframe formatted like this in pandas.

(df)
School ID      Column 1       Column 2     Column 3
School 1                      8100         8200  
School 2       9-999 
School 3                      9300         9500 
School 4                      7700         7800 
School 5       8999
....

I want to be able to enter a value, for example if the value is a direct hit of column 1 (of string type), such as Input Number: 9-999, it would return

Input Number: 9-999

School ID     Column 1
School 2      9-999

But if the number entered is between the numbers in Column 2 and 3 (floats), I'd like to return the associated School ID, such as this.

Input Number: 8110

School ID      Column 2    Column 3
School 5       8100        8200

Right now I have this code:

def find(num) :
d1=df.loc[df['Column 1']==num]
if len(d1)>0 :
    return d1[['School ID','Column 1']]
else :
    return df.loc[(num>= df['Column 2']) & (num<= df['Column 3'])][['School ID','Column 2','Column 3']]

But I am getting an error saying: 'return' outside function

Thanks for the help.

4
  • Looks like an indentation error Commented Oct 20, 2020 at 17:36
  • Just updated it to show the correct indentation, still giving me that error. Commented Oct 20, 2020 at 17:38
  • It still isn't fixed Commented Oct 20, 2020 at 17:39
  • Correct, it is not. Commented Oct 20, 2020 at 17:41

1 Answer 1

1
s = '9-999'

q = df[df['Column_1']==str(s)]

if len(q):
    print(q)
else:
    m = df[['Column_2', 'Column_3']].apply(lambda x: x['Column_2'] <= s <= x['Column_3'], axis=1)
    print(df[m])

Prints:

  School_ID Column_1  Column_2  Column_3
1  School 2    9-999       0.0       0.0

For s = 8110:

  School_ID Column_1  Column_2  Column_3
0  School 1        0    8100.0    8200.0

EDIT: To have consistent datatypes, you can convert Column2 and Column3 to float:

s = '8110'

q = df[df['Column_1']==str(s)]

df['Column_2'] = df['Column_2'].astype(float)
df['Column_3'] = df['Column_3'].astype(float)

if len(q):
    print(q)
else:
    s = float(s)
    m = df[['Column_2', 'Column_3']].apply(lambda x: x['Column_2'] <= s <= x['Column_3'], axis=1)
    print(df[m])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help! But now I am getting : TypeError: '<=' not supported between instances of 'numpy.ndarray' and 'str'
@DerekFisher It's probably necessary to convert the s variable to float, and Column_2 and Column_3 as well. See my edit.
was missing the s = float(s), I should have noticed. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.