Home > Enterprise >  Is there any pythonic way (without iteration) to apply a math function in each cell values and then
Is there any pythonic way (without iteration) to apply a math function in each cell values and then

Time:06-03

For example, I have a dataframe that looks like this:

column1 column2 column3 column(n) result
2.3 3.4 4.5 ... ?
1 2 3 ... ?
... ... ... ... ?

For the two rows (in real m rows, hence a solution for arbitrary number of rows is required) I want a scalar value for EACH row in result column that will give the result in a new column applying the below formula:

equation

I can't figure out a concise way to do this with pandas without iteration of each cell; I want to do it in one line (pythonic way). Any help is greatly appreciated, thanks.

CodePudding user response:

I think this should work:

df['result'] = df.mul(np.log(df)).sum(axis=1) / np.sqrt(len(df.columns))

Output:

>>> df
   column1  column2  column3    result
0      2.3      3.4      4.5  7.415992
1      1.0      2.0      3.0  2.703230
  • Related