1

I have two DataFrame objects which I want to apply an element-wise multiplication on each row onto:

df_prob_wc.shape  # (3505, 13)
df_prob_c.shape   # (13, 1)

I thought I could do it with DataFrame.apply()

df_prob_wc.apply(lambda x: x.multiply(df_prob_c), axis=1)

which gives me:

TypeError: ("'int' object is not iterable", 'occurred at index $')

or with

df_prob_wc.apply(lambda x: x * df_prob_c, axis=1)

which gives me:

TypeError: 'int' object is not iterable

But it's not working. However, I can do this:

df_prob_wc.apply(lambda x: x * np.asarray([1,2,3,4,5,6,7,8,9,10,11,12,13]), axis=1)

What am I doing wrong here?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

1

It seems you need multiple by Series created with df_prob_c by iloc:

df_prob_wc = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df_prob_wc)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

df_prob_c = pd.DataFrame([[4,5,6,1,2,3]])
#for align data same columns in both df
df_prob_c.index = df_prob_wc.columns
print (df_prob_c)
   0
A  4
B  5
C  6
D  1
E  2
F  3

print (df_prob_wc.shape)
(3, 6)
print (df_prob_c.shape)
(6, 1)
print (df_prob_c.iloc[:,0])
A    4
B    5
C    6
D    1
E    2
F    3
Name: 0, dtype: int64

print (df_prob_wc.mul(df_prob_c.iloc[:,0], axis=1))
    A   B   C  D   E   F
0   4  20  42  1  10  21
1   8  25  48  3   6  12
2  12  30  54  5  12   9

Another solution is multiple by numpy array, only need [:,0] for select:

print (df_prob_wc.mul(df_prob_c.values[:,0], axis=1))

    A   B   C  D   E   F
0   4  20  42  1  10  21
1   8  25  48  3   6  12
2  12  30  54  5  12   9

And another solution with DataFrame.squeeze:

print (df_prob_wc.mul(df_prob_c.squeeze(), axis=1))
    A   B   C  D   E   F
0   4  20  42  1  10  21
1   8  25  48  3   6  12
2  12  30  54  5  12   9
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Nice! In my case I just had to transform `df_prob_c` and it worked: `df_prob_wc.mul(df_prob_c.T.iloc[0], axis=1)`. The solution I came up with was `df_prob_wc.apply(lambda x: x * df_prob_c.T.as_matrix()[0], axis=1)` but yours is much simpler of course. Thanks! – Stefan Falk Mar 04 '17 at 13:07
  • Yes, apply here is much slowier, better is use `mul`. – jezrael Mar 04 '17 at 13:07
  • 1
    and now I change solution, you need `df_prob_wc.mul(df_prob_c.iloc[:, 0], axis=1)` – jezrael Mar 04 '17 at 13:08
  • 1
    Thanks for that explanation! – Stefan Falk Mar 04 '17 at 14:23