numpy判断数值类型、过滤出数值型数据的方法

yipeiwu_com5年前Python基础

numpy是无法直接判断出由数值与字符混合组成的数组中的数值型数据的,因为由数值类型和字符类型组成的numpy数组已经不是数值类型的数组了,而是dtype='<U11'。

1、math.isnan也不行,它只能判断float("nan"):

>>> import math 
>>> math.isnan(1) 
False 
>>> math.isnan('a') 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
TypeError: a float is required 
>>> math.isnan(float("nan")) 
True 
>>> 

2、np.isnan不可用,因为np.isnan只能用于数值型与np.nan组成的numpy数组:

>>> import numpy as np 
>>> test1=np.array([1,2,'aa',3]) 
>>> np.isnan(test1) 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
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 ''sa 
fe'' 
>>> test2=np.array([1,2,np.nan,3]) 
>>> np.isnan(test2) 
array([False, False, True, False], dtype=bool) 
>>> 

解决办法:

方法1:将numpy数组转换为python的list,然后通过filter过滤出数值型的值,再转为numpy, 但是,有一个严重的问题,无法保证原来的索引

>>> import numpy as np 
>>> test1=np.array([1,2,'aa',3]) 
>>> list1=list(test1) 
>>> def filter_fun(x): 
... try: 
...  return isinstance(float(x),(float)) 
... except: 
...  return False 
... 
>>> list(filter(filter_fun,list1)) 
['1', '2', '3'] 
>>> np.array(filter(filter_fun,list1)) 
array(<filter object at 0x0339CA30>, dtype=object) 
>>> np.array(list(filter(filter_fun,list1))) 
array(['1', '2', '3'], 
 dtype='<U1') 
>>> np.array([float(x) for x in filter(filter_fun,list1)]) 
array([ 1., 2., 3.]) 
>>> 

方法2:利用map制作bool数组,然后再过滤数据和索引:

>>> import numpy as np
>>> test1=np.array([1,2,'aa',3])
>>> list1=list(test1)
>>> def filter_fun(x):
... try:
...  return isinstance(float(x),(float))
... except:
...  return False
...
>>> import pandas as pd
>>> test=pd.DataFrame(test1,index=[1,2,3,4])
>>> test
 0
1 1
2 2
3 aa
4 3
>>> index=test.index
>>> index
Int64Index([1, 2, 3, 4], dtype='int64')
>>> bool_index=map(filter_fun,list1)
>>> bool_index=list(bool_index) #bool_index这样的迭代结果只能list一次,一次再list时会是空,所以保存一下list的结果
>>> bool_index
[True, True, False, True]
>>> new_data=test1[np.array(bool_index)]
>>> new_data
array(['1', '2', '3'],
 dtype='<U11')
>>> new_index=index[np.array(bool_index)]
>>> new_index
Int64Index([1, 2, 4], dtype='int64')
>>> test2=pd.DataFrame(new_data,index=new_index)
>>> test2
 0
1 1
2 2
4 3
>>>

以上这篇numpy判断数值类型、过滤出数值型数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中py文件引用另一个py文件变量的方法

最近自己初学Python,在编程是遇到一个问题就是,怎样在一个py文件中使用另一个py文件中变量,问题如下: demo1代码 import requests r = requests...

python实现可视化动态CPU性能监控

python实现可视化动态CPU性能监控

本文实例为大家分享了python可视化动态CPU性能监控的具体代码,供大家参考,具体内容如下 打算开发web性能监控,以后会去学js,现在用matp来补救下,在官网有此类模板,花了一点时...

Python从list类型、range()序列简单认识类(class)【可迭代】

本文实例讲述了Python从list类型、range()序列简单认识类(class)。分享给大家供大家参考,具体如下: list类型 定义: items = [] 这就定义了一...

利用Python实现简单的相似图片搜索的教程

利用Python实现简单的相似图片搜索的教程

大概五年前吧,我那时还在为一家约会网站做开发工作。他们是早期创业公司,但他们也开始拥有了一些稳定用户量。不像其他约会网站,这家公司向来以洁身自好为主要市场形象。它不是一个供你鬼混的网站—...

在Python的Django框架中创建和使用模版

如何使用模板系统 让我们深入研究模板系统,你将会明白它是如何工作的。但我们暂不打算将它与先前创建的视图结合在一起,因为我们现在的目的是了解它是如何独立工作的。 。 (换言之, 通常你会将...