0

I want to apply a formula to a dataframe. My columns has two index. So while applying the formula I am getting error

ValueError: Wrong number of items passed 26, placement implies 1

this is my formula

df61[('average_molecular_mass', 'mol')] = df61.apply(lambda x: etll(x[('CTG Barometric pressure','inHg')], x[('Ambient temperature','Deg F')], x[('Relative Humidity','%')]), axis=1)

This is how my dataframe look like enter image description here

My columns name as below

MultiIndex([(                              'Date',   'Time'),
            (                      'HRSG DB Flow',   'T/Hr'),
            (                          'CTG Load',     'MW'),
            (                  'CTG Exhaust Mass', 'Kg/Sec'),
            (           'CTG Exhaust Temperature',  'Deg F'),
            (           'CTG Barometric pressure',   'inHg'),
            (              'Ambient temperature ',  'Deg F'),
            (                 'Relative Humidity',      '%'),
            (             'Compressor inlet temp',  'Deg F'),
            (                     'CTG Fuel flow', 'lb/sec'),
            (                           'CTG IGV',      '%'),
            (        'Methane ( CH4 )composition',   'MOL%'),
            (          'Ethane(C2H6) composition',   'MOL%'),
            (         'Propane(C3H8) composition',   'MOL%'),
            (                          'Nitrogen',   'MOL%'),
            (                    'Carbon dioxide',   'MOL%'),
            (                        'Stack Temp',  'Deg C'),
            (          'Lp Economiser Inlet Flow',    'TPH'),
            (          'Lp Economiser Inlet Temp',  'Deg C'),
            ('Condensate pump discharge pressure',   'kPaG'),
            (                     'Lp Steam Flow',    'TPH'),
            (                     'Lp Steam Temp',  'Deg C'),
            (                 'LP Steam Pressure',   'kPaG'),
            (                     'HP Steam Flow',    'TPH'),
            (                     'HP Steam Temp',  'Deg C'),
            (                 'HP Steam Pressure',   'kPaG')],
           )
df = pd.DataFrame({('CTG Barometric pressure','inHg'): [30.0,30.0,30.0,30.0,30.0],
('Ambient temperature','Deg F'): [73.9,74.5,74.5,74.5,73.9],
('Relative Humidity','%'): [51.2,50.4,50.1,49.6,50.0]})

Original data frame

df[('average_molecular_mass', 'mol')] = df.apply(lambda x: etll(x[('CTG Barometric pressure','inHg')], x[('Ambient temperature','Deg F')], x[('Relative Humidity','%')]), axis=1)

After applying function:

df = pd.DataFrame({('CTG Barometric pressure','inHg'): [30.0,30.0,30.0,30.0,30.0],
('Ambient temperature','Deg F'): [73.9,74.5,74.5,74.5,73.9],
('Relative Humidity','%'): [51.2,50.4,50.1,49.6,50.0],
('average_molecular_mass', 'mol'): [28.80745690217542, 28.807, 28.807,28.809,28.808]})

Outcome after applying function

etll formula:

def etll(x, y, z):
try:
        input1 = x * 0.491154
        input2 = y +459.67
        input3 = input2**2
        input4 = input2**3 
        input5 = input2**4 
        input6 = math.log(input2)
        c1 = -10440.397 # constant
        c2 = -11.29465 # constant
        c3 = -0.027022355 # constant
        c4 = 0.00001289036 # constant
        c5 = -0.0000000024780681 # constant
        c6 = 0 # constant
        c7 = 6.5459673 # constant 
        ln = (c1/input2)+ c2 + (c3*input2) + (c4*input3) + (c5*input4) + (c6*input5) + (c7*input6)
        Pv = math.exp(ln)
        ph = z/100*Pv
        percentage_ratio = (input1-ph)/input1
        dervied_ratio = (1/percentage_ratio-1)*(18.01528/28.96518)
        constant1 = 28.01348 
        constant2 = 31.9988
        constant3 = 44.0098
        constant4 = 18.01528 
        constant5 = 39.948 
        output1 = 0.78084*percentage_ratio
        output2 = 0.209476*percentage_ratio
        output3 = 0.000319* percentage_ratio
        output4 = 1- percentage_ratio
        output5 = 0.009365*percentage_ratio
        output6 = output1 * constant1
        output7 = output2 * constant2
        output8 = output3 * constant3
        output9 = output4 * constant4
        output10 = output5 * constant5
        average_molecular_mass = (output6+ output7 + output8 
                                  + output9 + output10)
        return average_molecular_mass
    except (ValueError, TypeError, ZeroDivisionError, KeyError):
        pass

I think it is related to multi index assignment but not sure where I am making the mistake.

Can some one please advise on this?

Thank you for your time.

Timus
  • 10,974
  • 5
  • 14
  • 28
DBV
  • 25
  • 5
  • Please post a minimum workable example with dummy dataframe. – le_camerone Mar 30 '22 at 05:34
  • Hi, I have posted some more details in my question. Can you please look in to it? – DBV Mar 30 '22 at 06:28
  • 1
    You might need to provide a dummy dataframe so that it can be replicated. See for example this question: https://stackoverflow.com/q/71671418/2429187 While the question itself is a duplicate, the asker has provided a dataframe for the answer to work on. Otherwise it is too difficult to replicate. – le_camerone Mar 30 '22 at 06:38
  • Thanks for sharing that. Added working dataframe. – DBV Mar 30 '22 at 07:11
  • Could you please also share the function etll you are using in the `apply` statement ? – Rabinzel Mar 30 '22 at 07:19
  • @Rabinzel I have copied etll formula for reference. Thanks. – DBV Mar 30 '22 at 07:57
  • do you also get the error when running your dummy dataframe ? Because for me it works and I get the expected output. – Rabinzel Mar 30 '22 at 08:13
  • @Rabinzel I am getting error on dummy dataframe.df[('average_molecular_mass', 'mol')] = df.apply(lambda x: etll(x[('CTG Barometric pressure','inHg')], x[('Ambient temperature','Deg F')], x[('Relative Humidity','%')], axis=1) ^ SyntaxError: invalid syntax can you please check what mistake I am making? – DBV Mar 30 '22 at 08:57
  • There's a closing `)` missing before `, axis=1)` – Timus Mar 30 '22 at 09:29
  • Thanks there were more brackets missing along with this one. Now it is fine. I couldn't find it out in jupyter so tried in VS code . – DBV Mar 30 '22 at 10:06

0 Answers0