python - Multiplying multiple columns in a DataFrame -


i'm trying multiply n columns in dataframe n columns in same dataframe, , divide results single column. i'm having trouble first part, see example below.

import pandas pd numpy import random  foo = pd.dataframe({'a':random.rand(10),                     'b':random.rand(10),                     'c':random.rand(10),                     'n':random.randint(1,100,10),                     'x':random.rand(10),                     'y':random.rand(10),                     'z':random.rand(10), })  foo[['a','b','c']].multiply(foo[['x','y','z']], axis=0).divide(foo['n'], axis=0) 

what i'm trying @ column-wise multiplication (i.e. a*x, b*y, c*z) result not n column matrix 2n one, columns i'm trying multiply added dataframe, , entries have nan values, so:

      b   c   x   y   z 0 nan nan nan nan nan nan 1 nan nan nan nan nan nan 2 nan nan nan nan nan nan 3 nan nan nan nan nan nan 4 nan nan nan nan nan nan 5 nan nan nan nan nan nan 6 nan nan nan nan nan nan 7 nan nan nan nan nan nan 8 nan nan nan nan nan nan 9 nan nan nan nan nan nan 

what's going on here, , how do column-wise multiplication?

this work using values columns x, y, z , n, perhaps see issue is:

>>> (foo[['a','b','c']]      .multiply(foo[['x','y','z']].values)      .divide(foo['n'].values, axis=0))                   b         c 0  0.000452  0.004049  0.010364 1  0.004716  0.001566  0.012881 2  0.001488  0.000296  0.004415 3  0.000269  0.001168  0.000327 4  0.001386  0.008267  0.012048 5  0.000084  0.009588  0.003189 6  0.000099  0.001063  0.006493 7  0.009958  0.035766  0.012618 8  0.001252  0.000860  0.000420 9  0.006422  0.005013  0.004108 

the result indexed on columns a, b, c. unclear resulting columns should be, why getting nans.

appending function above .values give result desire, replace index , columns.

>>> (foo[['a','b','c']]      .multiply(foo[['x','y','z']].values)      .divide(foo['n'].values, axis=0)).values array([[  4.51754797e-04,   4.04911292e-03,   1.03638836e-02],        [  4.71588457e-03,   1.56556402e-03,   1.28805803e-02],        [  1.48820116e-03,   2.95700572e-04,   4.41516179e-03],        [  2.68791866e-04,   1.16836123e-03,   3.27217820e-04],        [  1.38648301e-03,   8.26692582e-03,   1.20482313e-02],        [  8.38762247e-05,   9.58768066e-03,   3.18903965e-03],        [  9.94132918e-05,   1.06267623e-03,   6.49315435e-03],        [  9.95764539e-03,   3.57657737e-02,   1.26179014e-02],        [  1.25210929e-03,   8.59735215e-04,   4.20124326e-04],        [  6.42175897e-03,   5.01250179e-03,   4.10783492e-03]]) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -