NaN 값을 확인하려면 어떻게 해야 하나요?
float('nan')
는 NaN(숫자가 아님)을 나타냅니다.하지만 어떻게 확인할 수 있죠?
사용방법:
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
NaN을 테스트하는 일반적인 방법은 NaN이 자신과 동일한지 확인하는 것입니다.
def isNaN(num):
return num != num
numpy.isnan(number)
그것이 맞다면NaN
그렇지 않으면.
변수가 "NaN"인지 여부를 검정할 수 있는 세 가지 방법이 있습니다.
import pandas as pd
import numpy as np
import math
# For single variable all three libraries return single boolean
x1 = float("nan")
print(f"It's pd.isna: {pd.isna(x1)}")
print(f"It's np.isnan: {np.isnan(x1)}}")
print(f"It's math.isnan: {math.isnan(x1)}}")
산출량
It's pd.isna: True
It's np.isnan: True
It's math.isnan: True
다음은 관련된 답변입니다.
- IEEE 754 표준을 준수하는 NaN 구현
- 즉, python의 NaN:
float('nan')
,numpy.nan
...
- 즉, python의 NaN:
- 기타 오브젝트: 문자열 또는 기타 (예외가 발생했을 경우 발생하지 않음)
표준에 따라 구현된 NaN은 그 자체와의 불평등 비교에서 True를 반환해야 하는 유일한 값이다.
def is_nan(x):
return (x != x)
몇 가지 예를 들 수 있습니다.
import numpy as np
values = [float('nan'), np.nan, 55, "string", lambda x : x]
for value in values:
print(f"{repr(value):<8} : {is_nan(value)}")
출력:
nan : True
nan : True
55 : False
'string' : False
<function <lambda> at 0x000000000927BF28> : False
자신과 동일한지 확인하는 것은
x!=x
가장 빠릅니다.
import pandas as pd
import numpy as np
import math
x = float('nan')
%timeit x!=x
44.8 ns ± 0.152 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit math.isnan(x)
94.2 ns ± 0.955 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit pd.isna(x)
281 ns ± 5.48 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit np.isnan(x)
1.38 µs ± 15.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
사실 방금 마주쳤지만, 저는 nan, -inf, inf를 체크하고 있었어요.그냥 사용했어요
if float('-inf') < float(num) < float('inf'):
이는 숫자에 대해, nan 및 두 inf에 대해 모두 해당되며 문자열이나 다른 유형(아마도 좋은 것일 수 있음)에 대해 예외가 발생합니다.또한 math나 numpy와 같은 라이브러리를 가져올 필요가 없습니다(numpy는 컴파일된 응용 프로그램의 크기가 두 배가 될 정도로 매우 큽니다).
그 숫자와 비교합니다.NaN은 항상 != NaN입니다. 그렇지 않으면 (예를 들어 수치인 경우) 비교가 성공해야 합니다.
기능에 문제가 있어서 이 게시물에 들어갔습니다.
math.isnan()
이 코드를 실행하면 문제가 발생합니다.
a = "hello"
math.isnan(a)
그것은 예외를 일으킨다.이에 대한 저의 해결책은 다음 사항을 확인하는 것입니다.
def is_nan(x):
return isinstance(x, float) and math.isnan(x)
2.6 미만이 되어도 numpy가 없고 IEEE 754가 지원되지 않는 경우의 다른 방법:
def isNaN(x):
return str(x) == str(1e400*0)
python < 2 . 6 python 。
def isNaN(x):
return str(float(x)).lower() == 'nan'
Solaris 5.9 박스의 python 2.5.1과 Ubuntu 10의 python 2.6.5에서 사용할 수 있습니다.
웹 서비스로부터 데이터를 수신하고 있습니다.NaN
끈으로'Nan'
하지만 내 데이터에는 다른 종류의 문자열이 있을 수 있기 때문에float(value)
예외를 던질 수 있습니다.저는 다음과 같은 다양한 답변을 사용했습니다.
def isnan(value):
try:
import math
return math.isnan(float(value))
except:
return False
요건:
isnan('hello') == False
isnan('NaN') == True
isnan(100) == False
isnan(float('nan')) = True
변수가 NaN인지 None인지를 판별하는 모든 방법:
유형 없음
In [1]: from numpy import math
In [2]: a = None
In [3]: not a
Out[3]: True
In [4]: len(a or ()) == 0
Out[4]: True
In [5]: a == None
Out[5]: True
In [6]: a is None
Out[6]: True
In [7]: a != a
Out[7]: False
In [9]: math.isnan(a)
Traceback (most recent call last):
File "<ipython-input-9-6d4d8c26d370>", line 1, in <module>
math.isnan(a)
TypeError: a float is required
In [10]: len(a) == 0
Traceback (most recent call last):
File "<ipython-input-10-65b72372873e>", line 1, in <module>
len(a) == 0
TypeError: object of type 'NoneType' has no len()
NaN 타입
In [11]: b = float('nan')
In [12]: b
Out[12]: nan
In [13]: not b
Out[13]: False
In [14]: b != b
Out[14]: True
In [15]: math.isnan(b)
Out[15]: True
Python 3.6에서 문자열 값 x math.isnan(x) 및 np.isnan(x)을 확인하면 오류가 발생합니다.그래서 NaN인지 아닌지 미리 모르면 확인할 수 없습니다.다음은 이 문제를 해결하는 것 같습니다.
if str(x)=='nan' and type(x)!='str':
print ('NaN')
else:
print ('non NaN')
★★pd.isna
,math.isnan
★★★★★★★★★★★★★★★★★」np.isnan
다양한 유형의 물체를 다루는 유연성이 있습니다.
다음 표에 지정된 메서드로 오브젝트 유형을 확인할 수 있는지 여부를 나타냅니다.
+------------+-----+---------+------+--------+------+
| Method | NaN | numeric | None | string | list |
+------------+-----+---------+------+--------+------+
| pd.isna | yes | yes | yes | yes | yes |
| math.isnan | yes | yes | no | no | no |
| np.isnan | yes | yes | no | no | yes | <-- # will error on mixed type list
+------------+-----+---------+------+--------+------+
pd.isna
다양한 유형의 결측값을 확인하는 가장 유연한 방법입니다.
.pd.isna
. 하는 동안에math.isnan
★★★★★★★★★★★★★★★★★」np.isnan
True
★★★★★★에NaN
값에서는 , 음, 음, 음, 다, 다, 다, 습, 습, 습, 습, 습, 습, 습, 습, 습, 습, 습, 습, 습, values, values, values, values, values, values 등의 다른None
는는문문 문다다다다다두 방법 모두 오류를 반환하기 때문에 혼합된 유형의 목록을 확인하는 것은 번거롭습니다. ★★★★★★★★★★★★★★★.pd.isna
합니다.
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: missing_values = [3, None, np.NaN, pd.NA, pd.NaT, '10']
In [4]: pd.isna(missing_values)
Out[4]: array([False, True, True, True, True, False])
혼합 데이터 유형 목록에서 NaN(플로트) 항목을 제거하는 방법
반복 가능한 타입에 혼합 타입이 있는 경우는, numpy 를 사용하지 않는 솔루션을 다음에 나타냅니다.
from math import isnan
Z = ['a','b', float('NaN'), 'd', float('1.1024')]
[x for x in Z if not (
type(x) == float # let's drop all float values…
and isnan(x) # … but only if they are nan
)]
['a', 'b', 'd', 1.000]
란 '단락하다'라는 입니다.isnan
다음과 같이 타입 'syslog'가 아닌 값에서는 호출되지 않습니다.False and (…)
False
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
nan 타입 플로트의 경우
>>> import pandas as pd
>>> value = float(nan)
>>> type(value)
>>> <class 'float'>
>>> pd.isnull(value)
True
>>>
>>> value = 'nan'
>>> type(value)
>>> <class 'str'>
>>> pd.isnull(value)
False
pd.isnull: 팬더 스트링에 대한 pd.isnull:
if not pd.isnull(atext):
for word in nltk.word_tokenize(atext):
NLTK의 특징 추출 기능
def act_features(atext):
features = {}
if not pd.isnull(atext):
for word in nltk.word_tokenize(atext):
if word not in default_stopwords:
features['cont({})'.format(word.lower())]=True
return features
언급URL : https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-values
'programing' 카테고리의 다른 글
Larabel에서 HTTP Request 본문 콘텐츠를 가져오려면 어떻게 해야 하나요? (0) | 2022.09.30 |
---|---|
Java 소스 파일의 일부를 Kotlin으로 변환하려면 어떻게 해야 합니까? (0) | 2022.09.30 |
Java에서 인덱스에서 Enum 값을 가져오려면 어떻게 해야 합니까? (0) | 2022.09.30 |
JavaScript를 사용하여 이미지의 실제 폭과 높이를 확인하시겠습니까?(Safari/Chrome) (0) | 2022.09.23 |
상태 배열에서 항목을 삭제하는 방법 (0) | 2022.09.23 |