2016-06-30 62 views
1

我有一個dataframes稱爲historic_price有兩列名爲'price''item',我想多一起存儲在不同的數據幀稱爲dayData的結果,但我得到的異常:轉換一個數據幀列浮動

TypeError: can't multiply sequence by non-int of type 'float' 

'price'柱看起來像:

  price 
0  5.86500 
1  2.03000 
2  13.55000 
3  639.75450 
4  343.94325 
5  1009.43500 
6  585.60600 
7  2208.72400 
8  807.54800 
9  236.51530 
10  14.34000 

'item'柱看起來像:

 item 
0  0.0 
1  0.0 
2  0.0 
3  0.0 
4  0.0 
5  0.0 
6  0.0 
7  0.0 
8  0.0 
9  0.0 
10 0.0 

(我知道所有的值都爲零,但即使是這樣,當我多次價格由項目我768,16仍然得到一個結果(0))

兩個列中的數據類型都<class 'pandas.core.series.Series'>

我試圖項目和價格的產品添加到dayData數據框如下:

dayData["cash"] = historicPrice["price"] * historicPrice["item"] 

,但我得到上面的例外。

我試圖將列浮動:

dayData["cash"] = float(historicPrice["price"]) * float(historicPrice["item"]) 

,但沒有運氣(我得到異常:TypeError: cannot convert the series to <class 'float'>

任何人都可以讓我知道我需要做什麼來解決嗎?

非常感謝

+1

「historicPrice.dtypes」的結果是什麼? –

+1

你需要轉換dtypes嘗試'df = df.astype(float)'第一個 – EdChum

+0

dtype:object,我試着historicPower = historicPower.astype(float)但得到了這個異常:TypeError:不能乘以非int的序列的類型'浮動',當我然後嘗試乘以列一起 – Stacey

回答

4

很有可能您在其中一列中有字符串。嘗試使用to_numeric,並將錯誤設置爲'脅迫':

df['price'] = pd.to_numeric(df['price'], errors='coerce').fillna(0) 
df['item'] = pd.to_numeric(df['item'], errors='coerce').fillna(0) 

df['cash'] = df['price'] * df['item']