0

I am trying to multiplicate a whole dataframe size 40 row * 600 columns by a pandas.core.series.Series with 40 row and only one columns. So my goal is to multiply all the rows by the unique row. And it gives me back an error.

operands could not be broadcast together with shapes (23560,) (589,)

[In] df1:
[out]
Index             col1 
2065-12-20     12 days
2061-10-31     12 days
2045-11-28     70 days 
2043-10-31     11 days
2040-07-30     21 days
2049-06-30     64 days 
2036-01-31     14 days 

[In] df2: 
Index             col1    col2   etc.... 
2065-12-20         14      120
2061-10-31         18      800
2045-11-28         19      580
2043-10-31         21      12
2040-07-30         44      21
2049-06-30         1.2     17
2036-01-31         61.8    61 


[in] k = df1 * df2
[out] operands could not be broadcast together with shapes (23560,) (589,)

I ultimately want

 Index             col1        col2   etc.... 
2065-12-20         14*12      120*12
2061-10-31         18*12      800*12
2045-11-28         19*70      580*70
2043-10-31         21*11      12*11
2040-07-30         44*21      21*21
2049-06-30         1.2*64     17*64
2036-01-31         61.8*14    61*61

It is probably very basic but I am stugling with it..is it because my df1 is in days? How can I transform it into regular numbers? Thank you

user6457870
  • 247
  • 5
  • 14
  • what I can think of is extract the first df into a series and apply this multiplication to the second df. But I don't think its elegant :( – Bobby Apr 30 '17 at 14:40
  • @Bobby Still not working, I tried... `operands could not be broadcast together with shapes (23560,) (589,)` – user6457870 Apr 30 '17 at 14:42

1 Answers1

3

Use the mul method to execute an element-wise multiplication between two DataFrames:

k = df1.mul(df2)

If you're still having trouble due to the first DataFrame having the column in days, then you can convert it to an int or float before performing the element-wise multiplication step:

import numpy as np
df1.col1 = (df1.col1.values / np.timedelta64(1, 'D')).astype(int)
Community
  • 1
  • 1
mgig
  • 2,395
  • 4
  • 21
  • 36
  • thank you, I am trying to conserve the same ammount of days but just erasing the fact its day. the `41 days` it want to transform it in `41`, is there a way? – user6457870 Apr 30 '17 at 14:53
  • is the `41 days` a pandas Timedelta type, or a string? – mgig Apr 30 '17 at 14:57
  • a Timedelta64 type – user6457870 Apr 30 '17 at 14:58
  • df1 is actually a `pandas.core.series.Series` – user6457870 Apr 30 '17 at 15:04
  • I just updated the solution above to convert col1 to days in int form. try that out – mgig Apr 30 '17 at 15:04
  • Thank you! Last problem, `'Timestamp' object is not iterable.` any ideas? – user6457870 Apr 30 '17 at 15:10
  • Do you get that message when attempting to convert col1 from days to int, or when trying to multiply the two dataframes together? If you type df1.dtypes, are there any non int64 or float64 values in the dataframe? – mgig Apr 30 '17 at 15:16
  • I have this message when I atempt to multiply it. df1.dtypes = `dtype('int32')` . df2.dtypes gives me back dtype: object – user6457870 Apr 30 '17 at 15:22
  • any ideas? My indexes were not the same but problem solved. Still have this `'Timestamp' object is not iterable.` then I try to do df1 * df2 it gives me df2 full of NaN – user6457870 Apr 30 '17 at 15:52
  • strange. can you provide some code above to generate two small example (just a few elements) dataframes/series that are the same as the ones you're working with so that I can try to reproduce the problem? – mgig Apr 30 '17 at 15:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143036/discussion-between-user6457870-and-mgig). – user6457870 Apr 30 '17 at 16:01