Pandas:在列中更改数据类型,然后乘以两列

时间:2013-10-11 16:22:54

标签: python pandas

我已将两个文件导入为DataFrames,并希望将“新价格”乘以“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'

我以为我改变了列的值......

1 个答案:

答案 0 :(得分:1)

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似乎不是其中之一。

相关问题