当前位置:网站首页>[Numpy] Numpy对于NaN值的判断
[Numpy] Numpy对于NaN值的判断
2022-06-24 03:35:00 【山茶花开时。】

numpy.nan的数据类型是float类型
import numpy as np
type(np.nan) # float
任何数字和numpy.nan进行计算,返回的结果都是nan
import numpy as np
print(np.nan + 1) # nan
print(np.nan - 1) # nan
print(np.nan * 1) # nan
print(np.nan / 1) # nan 对空值NaN的判断不能直接使用==表达式,bool表达式,以及不可直接使用if语句判断
import numpy as np
np.nan == np.nan # False
bool(np.nan) # True
# 输出结果:na is not null
if np.nan:
print('np.nan is not null') 需要使用Numpy自带的方法np.isnan(),is表达式,in表达式进行判断
import numpy as np
np.nan is np.nan # True
np.isnan(np.nan) # True
np.nan in [np.nan] # True
提示
如果使用Pandas来判断numpy.nan,可使用pd.isnull(),pd.isna()
import numpy as np import pandas as pd pd.isnull(np.nan) # True pd.isna(np.nan) # True
注意:None、NaN、''空字符串的区别
# None是Python的特殊类型
# NoneType对象,它只有一个值None
type(None) # NoneType
None == None # True
None == np.nan # False
# 空字符串''
type('') # strPandas中的pd.isnull不仅可以检测np.nan也可以检测None,但不可以检测字符串,比如'','nan','None'
import pandas as pd
import numpy as np
pd.isnull(np.nan) # True
pd.isnull(None) # True
pd.isnull('') # False
pd.isnull('np.nan') # False
pd.isnull('None') # Falseimport pandas as pd
import numpy as np
List = ['nan', '', 'None', None, np.nan]
for i in List:
if i == '' or pd.isnull(i) or pd.isnull(float('nan')) or i == 'None':
print(i)
# 上述输出结果:
# nan
# ''
# None
# None
# nanList(5 elements)

边栏推荐
- Under what circumstances do you need a fortress machine? What are the functions of a fortress machine
- Rasa 3. X learning series -rasa 3.2.0 new release
- News | detailed explanation of network security vulnerabilities of branch enterprises
- Why can't the fortress machine open the port? There is a problem with the use of the fortress machine port
- [competition experience sharing] design of intelligent guide rod
- hprofStringCache
- Which brand is a good backup all-in-one machine price
- Coding Ci of Devops
- MySQL stored procedure + function
- Summary of rust high concurrency programming
猜你喜欢
随机推荐
Live broadcast Reservation: cloud hosting or cloud function, how can the business do a good job in technology selection?
Typera cooperates with picgo to upload pictures to its own server with one click and obtain external links at the same time
Understand Devops from the perspective of leader
Modstartcms enterprise content site building system (supporting laravel9) v4.2.0
The importance of the computer room to the stable operation of the server
A Tencent interview question
How to build glasses website what are the functions of glasses website construction
Tke accesses the cluster through kubectl in pod
老弹出explorer.exe遇到问题已停止工作,怎么办?
Micro build low code enterprise exchange day · Shenzhen station opens registration
内存泄漏之KOOM
Grp: how to automatically add requestid in GRP service?
你了解TLS协议吗?
Do you understand TLS protocol?
Using RDM (Remote Desktop Manager) to import CSV batch remote
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
Recording a summary of frequently asked questions
Event id:7001: after restarting the machine, the World Wide Web failed to start automatically, resulting in inaccessible websites
How to solve the problem of easycvr playing the total recording time in the specified time period?
What is load balancing? What are the functions of load balancing?







