Home > Enterprise >  Trying to write a user_defined function in python that multiplies a formula of constants and coeff b
Trying to write a user_defined function in python that multiplies a formula of constants and coeff b

Time:12-05

I am trying to write a user defined function that takes median col values from a dataframe and places those values in a formula of constants and coefficients. I need the median col values to be multiplied one by one by the constants and coefficients. Below is what I need the function to do.

median = data[['col 1','col 2','col 3']].median()

col 1: 31.65
col 2: 87
col 3: 21.55

const_coeff = [(-.5447   .1712 * 31.65
   -.5447    .9601 * 87
   -.5447    .8474 * 21.55)]

print(constants_coefficients)

total sum of constants_coefficients 

.........................................................................................................

I have attempted many variations on the def function but unable to get the answer I get when plugging the values in manually. One example is below.

def i(median):
    const_coeff = 1
    for x in median:
        const_coeff = const_coeff * i
    return const_coeff

print(i(median))

The answer I get is negative number, which is wrong. Obviously, I used generic variables to show what I need/have done rather than my actual data so please forgive if that complicates things. Fairly new to coding and first-time poster. Thanks in advance for any help.

CodePudding user response:

Separate the values and plug them into the formula.

a,b,c = median.array
calc = [(-.5447   .1712 * a   -.5447    .9601 * b    -.5447    .8474 * c)]

Add parenthesis around the terms to ensure order of operations.


Or

q = median.array * (.1712,.9601,.8474)
# q = median.to_numpy() * (.1712,.9601,.8474)
q = q   (-.5447,-.5447,-.5447)
r = q.sum()

Pandas Series

  • Related