我已将两个文件导入为 DataFrame,并希望乘以“新价格”和“12 个月订购数量”。我虽然已经成功地将列从字符串更改为数字,以便能够将这两列相乘。而且我好像做错了什么。
我想更改数据类型,以便将两列相乘,然后将该列添加到 DataFrame 的末尾。
然后我想对相乘后的价格求和。
这是我失败的代码。
比较文件[['新价格']].convert_objects(convert_numeric =True) 比较文件[['12 个月订购数量']].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'
我以为我已经更改了列的值...
请您参考如下方法:
convert 函数不保留转换后的数据,而是将其返回。如果需要,您必须将其保存回旧数据。
Comparisonfile['New Price'] = Comparisonfile['New Price'].convert_objects(convert_numeric =True)
Comparisonfile['12 Month Quantity Ordered'] = Comparisonfile['12 Month Quantity Ordered'].convert_objects(convert_numeric =True)
pandas 中的许多函数都以这种方式运行。有些有 inplace 选项,尽管 convert_objects 似乎不是其中之一。
