May-17-2020, 02:02 PM
Hi all
I have the following dataframe that has been extracted from an Excel spreadsheet that I need to manipulate in code.
I know where the problem is (its in the commented line in the basemethod private method but I am running out of ideas on how to fix it. Some help would be much appreciated.
TIA
PS: Apologies, I clicked the post button in error, was meant to click the preview post button instead. Is there a way to remove the post if it has been posted accidentally?
Edit: Changed to csv instead to make troubleshooting easier.
I have the following dataframe that has been extracted from an Excel spreadsheet that I need to manipulate in code.
500Hz 1000Hz 1500Hz 2000Hz 3000Hz 4000Hz 6000Hz 8000Hz Right Ear (dB) 5 5 5 10 70 5 10 10 Left Ear (dB) 10 10 20 30 0 20 20 10Essentially what I need to do is extract a few columns and calculate the average dB for each ear. The result should be something like this:
3000Hz 4000Hz 6000Hz Average Right Ear (dB) 70 5 10 28.3 Left Ear (dB) 0 20 20 13.3The code below achieves this quite nicely:
import pandas as pd df = pd.read_csv('audio.csv) currentdf = df.loc[:,('3000Hz','4000Hz','6000Hz')] currentdf['average'] = (df.loc[:,'3000Hz'] + df.loc[:,'4000Hz'] + df.loc[:,'6000Hz'])/3 print(currentdf)Trouble is, I have five different combinations of different frequencies (columns) that I need to average out, each with different number of columns (e.g averaging the values of 500Hz and 1000Hz). Rather than repeating myself within each method within the class I will define a base method and derive the required combinations from that method. I tried the following but its giving me weird results:
import pandas as pd class audiogram(object): def __init__(self,audiodf): self.audiodf = audiodf def __basemethod(self,*frequency): newdf = df.loc[:,(f for f in frequency)] for f in frequency: newdf['average'] = (df[f].sum())/len(list(frequency)) #???? not sure how else to do it return newdf def first_criteria(self): print(self.__basemethod('3000Hz','4000Hz','6000Hz')) df = pd.read_csv('audio.csv') x = audiogram(df) x.first_criteria()The CSV file is:
,500Hz,1000Hz,1500Hz,2000Hz,3000Hz,4000Hz,6000Hz,8000Hz Right Ear (dB),5,5,5,10,70,5,10,10 Left Ear (dB),10,10,20,30,0,20,20,10I get the following output:
Output: 3000Hz 4000Hz 6000Hz average
Right Ear (dB) 70 5 10 23.333333
Left Ear (dB) 0 20 20 23.333333
....which is not what I am expecting. I know where the problem is (its in the commented line in the basemethod private method but I am running out of ideas on how to fix it. Some help would be much appreciated.
TIA
PS: Apologies, I clicked the post button in error, was meant to click the preview post button instead. Is there a way to remove the post if it has been posted accidentally?
Edit: Changed to csv instead to make troubleshooting easier.