2017-04-30 30 views
1

我正在嘗試將pandas.core.series.Series(df1 ['col1'])與pandas.core.frame.DataFrame(df2)相乘。他們的指標是相同的,但它總是給我回「‘時間戳’對象不是可迭代」當我試圖做'時間戳'對象不可迭代

k = df1['col1'].mul(df2) 


[In] df1: 
[out] 
Index   col1 col2 
2065-12-20  12  apples 
2061-10-31  12  candies 
2045-11-28  70  marshmalow 
2043-10-31  11  apples 
2040-07-30  21  cars 
2049-06-30  64  planes 
2036-01-31  14  glasses 



[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 

繁殖他們,我想

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 

的DF1 [」 COL1 ']是在天之前,我轉化它使用

df1['col1'] = (df1['col1'].values/np.timedelta64(1, 'D')).astype(int) 

df1.dtypes = D型(' INT32' )

任何想法爲什麼它拋出一個錯誤?

回答

1

例外來,因爲你有一個整體數據幀繁殖,不只是單個列(包括時間戳列!):

df1['col1'].mul(df2) 

鑑於你可能想要的數據幀:

df1['col1'].mul(df2['col1']) 

如果您想同時處理多列:

df1['col1'].values[:, None] * df2[['col1', 'col2', ...]] # explicitly list columns 

df1['col1'].values[:, None] * df2[df2.columns[1:]]  # all columns except the first 
+0

謝謝!我有最後一個問題,當我嘗試做分裂時,我有南無處不在。任何解釋? col1中有一個0,我嘗試用它分割整個df2。應該用'df2 [df2.columns [1:]]。div(df1 ['col1'])'工作嗎? – user6457870

+0

'NaN'在每一行中還是隻在'0'所在的行? – MSeifert

+0

無處不在。 .dtype是dtype('int32') – user6457870