Python 从同样包含对象的numpy数组中删除NaN行

Python 从同样包含对象的numpy数组中删除NaN行,python,numpy,Python,Numpy,给定一个numpy数组,我想确定哪些行包含NaN值和对象。 例如,一行将同时包含一个浮点值和一个列表 对于输入数组arr,我尝试执行arr[~np.isnanarr.anyaxis=1],但随后得到错误消息 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the ca

给定一个numpy数组,我想确定哪些行包含NaN值和对象。 例如,一行将同时包含一个浮点值和一个列表

对于输入数组arr,我尝试执行arr[~np.isnanarr.anyaxis=1],但随后得到错误消息

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could 
not be safely coerced to any supported types according to the casting rule ''safe''
isnan使用浮点数据类型数组;无法将对象数据类型转换为:

In [320]: np.isnan(x)                                                                                        
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-320-3b2be83a8ed7> in <module>
----> 1 np.isnan(x)

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
frompyfunc返回一个对象数据类型;让我们将其转换为布尔:

In [328]: np.frompyfunc(lambda i: i is np.nan,1,1)(x).astype(bool)                                           
Out[328]: 
array([[False, False,  True],
       [False, False, False]])
In [329]: np.any(_, axis=1)           # test whole rows                                                                       
Out[329]: array([ True, False])
In [330]: x[~_, :]                    # use that as mask to keep other rows                                                      
Out[330]: array([[3, list([5, 6, 7]), 8]], dtype=object)
另一个答案中建议的参数为空,可以进行类似的逐元素测试:

In [335]: pd.isnull(x)                                                                                       
Out[335]: 
array([[False, False,  True],
       [False, False, False]])

您可以尝试执行arr[~np.isnanarr,casting='no'。anyaxis=1]或arr[~np.isnanarr,casting='safe'。anyaxis=1]似乎数组中有不同的数据类型。np.isnan用于数字浮点型数组-因此,也许你可以预先选择数组中可以转换为浮点型的部分。应用np.isnan?@SayandipDutta,我尝试了你对np.array的建议[5.,np.nan,'asdf']-给我一个与@Anonymous experiences类似/相同的类型错误。给出了一个示例数组。我假设数据类型是object。例如,如果数组是np.array[[1[2,3],np.nan],[3[5,6,7],8],我想去掉第一行。
In [328]: np.frompyfunc(lambda i: i is np.nan,1,1)(x).astype(bool)                                           
Out[328]: 
array([[False, False,  True],
       [False, False, False]])
In [329]: np.any(_, axis=1)           # test whole rows                                                                       
Out[329]: array([ True, False])
In [330]: x[~_, :]                    # use that as mask to keep other rows                                                      
Out[330]: array([[3, list([5, 6, 7]), 8]], dtype=object)
In [335]: pd.isnull(x)                                                                                       
Out[335]: 
array([[False, False,  True],
       [False, False, False]])