numpy判斷數(shù)值類型、過(guò)濾出數(shù)值型數(shù)據(jù)的方法
numpy是無(wú)法直接判斷出由數(shù)值與字符混合組成的數(shù)組中的數(shù)值型數(shù)據(jù)的,因?yàn)橛蓴?shù)值類型和字符類型組成的numpy數(shù)組已經(jīng)不是數(shù)值類型的數(shù)組了,而是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不可用,因?yàn)閚p.isnan只能用于數(shù)值型與np.nan組成的numpy數(shù)組:
>>> 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數(shù)組轉(zhuǎn)換為python的list,然后通過(guò)filter過(guò)濾出數(shù)值型的值,再轉(zhuǎn)為numpy, 但是,有一個(gè)嚴(yán)重的問(wèn)題,無(wú)法保證原來(lái)的索引
>>> 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數(shù)組,然后再過(guò)濾數(shù)據(jù)和索引:
>>> 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這樣的迭代結(jié)果只能list一次,一次再list時(shí)會(huì)是空,所以保存一下list的結(jié)果 >>> 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判斷數(shù)值類型、過(guò)濾出數(shù)值型數(shù)據(jù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)將Excel內(nèi)容批量導(dǎo)出為PDF文件
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)將Excel表格內(nèi)容批量導(dǎo)出為PDF文件,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04使用PIL(Python-Imaging)反轉(zhuǎn)圖像的顏色方法
今天小編就為大家分享一篇使用PIL(Python-Imaging)反轉(zhuǎn)圖像的顏色方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01學(xué)會(huì)迭代器設(shè)計(jì)模式,幫你大幅提升python性能
這篇文章主要介紹了python 迭代器設(shè)計(jì)模式的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python __getattr__與__setattr__使用方法
__getattr__和__setattr__可以用來(lái)對(duì)屬性的設(shè)置和取值進(jìn)行處理2008-09-09python實(shí)現(xiàn)列車管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)列車管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Python利用緩存流實(shí)現(xiàn)壓縮PDF文件
在Python中,有許多庫(kù)可以用來(lái)壓縮PDF文件,其中最常用的是PyPDF2和PDFMiner,本文將為大家介紹一個(gè)新的方法,即使用緩存流壓縮PDF文件,感興趣的可以了解下2023-08-08