2017-04-30 149 views
0

我想乘以pandas.core.series.Series 40行和只有一列的整个数据帧大小40行* 600列。所以我的目标是将所有行乘以唯一行。它让我回来一个错误。熊猫:乘以数据帧

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,) 

我最终要

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它stugling因为我DF1是天?我怎样才能将它转化为常规数字? 谢谢

+0

什么,我能想到的是提取的第一个DF成串联和这个乘法应用到第二DF。但我不认为它的优雅:( – Bobby

+0

@Bobby仍然不工作,我试图...'操作数不能与形状(23560,)(589,)' – user6457870

回答

2

使用mul method两个DataFrames之间执行逐元素乘法:

k = df1.mul(df2) 

如果您仍遇到问题,由于第一数据帧有天塔,那么你就可以convert it to an int或执行逐元素相乘步骤之前浮动:

import numpy as np 
df1.col1 = (df1.col1.values/np.timedelta64(1, 'D')).astype(int) 
+0

广播谢谢,我试图保存同样的日子,但只是删除它的一天的事实。 '41天'它想要在'41'中转换它,有没有办法? – user6457870

+0

是'41天'一个熊猫Timedelta类型或字符串? – mgig

+0

一个Timedelta64类型 – user6457870