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.