Jul-12-2020, 02:06 PM
I'm trying to search if a dataframe contains a specified number for each row. The problem is: A user enters an audiogram, which is converted to a Dataframe in my code. I need to find if the highest number in each ear (row) is contained in a subset of the audiogram. The code is:
The audiogram:
My code:
When the code runs, I get:
Would appreciate some pointers.
Thanks in advance.
The audiogram:
Output: 500Hz 1000Hz 1500Hz 2000Hz 3000Hz 4000Hz 6000Hz 8000Hz
Right Ear 10 15 15 20 20 15 15 0
Left Ear 15 40 45 50 55 44 60 80
I want to know if the highest number in each ear (which is 20 on the right and 80 on the left) exists in the columns 3000Hz to 6000Hz inclusive. Should be True for right and False for left. My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import pandas as pd audio = pd.DataFrame([[ 10 , 15 , 15 , 20 , 20 , 15 , 15 , 0 ],[ 15 , 40 , 45 , 50 , 55 , 44 , 60 , 80 ]], index = [ 'Right Ear' , 'Left Ear' ], columns = [ '500Hz' , '1000Hz' , '1500Hz' , '2000Hz' , '3000Hz' , '4000Hz' , '6000Hz' , '8000Hz' ]) #generates audiogram above exclusion = [ '500Hz' , '1000Hz' , '1500Hz' , '2000Hz' , '8000Hz' ] df = audio.drop(labels = [i for i in exclusion], axis = 1 ) #gets rid of the columns in the exclusion list df2 = pd.DataFrame(data = audio. max (axis = 1 ), columns = [ 'maximum' ]) #gets max values for ear in df2.index: df2[ 'present in df' ] = True if (df.loc[(ear),:].isin(df2). any ()) else False #This is the line I am having trouble with print (df) print (df2) |
Output: 3000Hz 4000Hz 6000Hz #this is the result of print(df), the truncated dataframe from the original audiogram
Right Ear 20 15 15
Left Ear 55 44 60
maximum present in df
Right Ear 20 False #This should be True!
Left Ear 80 False # This is correct
...which is not what I expectedWould appreciate some pointers.
Thanks in advance.