Python Forum
Checking if an element belongs in a dataframe row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking if an element belongs in a dataframe row
#1
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:

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:
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)
When the code runs, I get:

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 expected

Would appreciate some pointers.

Thanks in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking for a recognized text in a Dataframe KDE 0 1,544 Aug-31-2021, 11:19 PM
Last Post: KDE
  Referring to a specific element in Pandas Dataframe Helmi 2 3,202 Mar-17-2019, 09:12 PM
Last Post: Helmi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020