Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用apply+清理熊猫中的数字数据;兰姆达_Python_Python 3.x_Pandas_Lambda_Series - Fatal编程技术网

Python 使用apply+清理熊猫中的数字数据;兰姆达

Python 使用apply+清理熊猫中的数字数据;兰姆达,python,python-3.x,pandas,lambda,series,Python,Python 3.x,Pandas,Lambda,Series,我正在清洗一些产品,发现线下有一个问题- feed_df['sale_price'] = feed_df['sale_price'].apply(lambda x: x if x > 0 else None) 这里我已经检查了销售价格的数据类型是object 错误是: TypeError: '>' not supported between instances of 'str' and 'int' 那么,如何更改lambda表达式来解决此问题呢?您所谓的“清理”实际上是使

我正在清洗一些产品,发现线下有一个问题-

    feed_df['sale_price'] = feed_df['sale_price'].apply(lambda x: x if x > 0 else None)
这里我已经检查了销售价格的数据类型是object

错误是:

TypeError: '>' not supported between instances of 'str' and 'int'
那么,如何更改lambda表达式来解决此问题呢?

您所谓的“清理”实际上是使数据变脏。熊猫专门从事矢量化操作。否则,使用Pandas就不会比使用值列表和字典好多少

这意味着您应该确保数字系列具有数字数据类型。可选的
object
dtype可以保存任意类型,但只不过是一系列指针,非常类似于
list
。另一方面,数字序列将作为连续的内存块保存在内存中。您将看到性能和内存使用方面的改进

在这种情况下,您可以使用
pd.to_numeric
errors='concurve'
,然后有条件地更新序列。无法转换为数字的值将作为
NaN
传递

feed_df['sale_price'] = pd.to_numeric(feed_df['sale_price'], errors='coerce')
feed_df.loc[feed_df['sale_price'] <= 0, 'sale_price'] = np.nan
feed_df['sale_price']=pd.to_numeric(feed_df['sale_price'],errors='concurve')

feed_df.loc[feed_df['sale_price']当您阅读错误消息时,您认为这是什么意思?sale price是字符串,我正在将它与整数进行比较。那么,什么是正确的方法呢?更改lambda表达式或在之前进行检查?感谢@jpp的解释。