我已經將兩個文件導入爲DataFrame,並希望乘以'新價格',但'12月訂購數量'。儘管如此,我已經成功地將字符串中的列更改爲數字,以便能夠將這兩列相乘。看來我做錯了什麼。熊貓:更改列中的數據類型,然後乘以兩列
我想改變數據類型,所以我可以乘以兩列,然後將該列添加到DataFrame的末尾。
然後我想得到相乘的價格。
這是我的代碼失敗了。
Comparisonfile[['New Price']].convert_objects(convert_numeric =True) Comparisonfile[['12 Month Quantity Ordered']].convert_objects(convert_numeric =True)
Comparisonfile[['12 Month Quantity Ordered']].convert_objects(convert_numeric =True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-69-e8d0f16b4286> in <module>()
----> 1 Comparisonfile['Proposed Ext. Price'] = Comparisonfile['New
Price']*Comparisonfile['12 Month Quantity Ordered']
C:\Anaconda2\lib\site-packages\pandas\core\series.pyc in wrapper(self, other, name)
162 if self.index.equals(other.index):
163 name = _maybe_match_name(self, other)
--> 164 return Series(wrap_results(na_op(lvalues, rvalues)),
165 index=self.index, name=name, dtype=dtype)
166
C:\Anaconda2\lib\site-packages\pandas\core\series.pyc in na_op(x, y)
72 if isinstance(y, pa.Array):
73 mask = notnull(x) & notnull(y)
---> 74 result[mask] = op(x[mask], y[mask])
75 else:
76 mask = notnull(x)
TypeError: can't multiply sequence by non-int of type 'float'
我想我已經改變了列的值...
你需要將轉換後的對象賦值給某個東西(它不在原地) – Jeff