DataFrame如何找出有空值的行
更新时间:2024年02月02日 10:31:09 作者:大地之灯
这篇文章主要介绍了DataFrame如何找出有空值的行问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
DataFrame找出有空值的行
import pandas as pdimport numpy as np
n = np.arange(20, dtype=float).reshape(5,4) n[2,3] = np.nan index = ['index1', 'index2', 'index3', 'index4', 'index5'] columns = ['column1', 'column2', 'column3', 'column4'] frame3 = pd.DataFrame(data=n, index=index, columns=columns)
frame3
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | 0.0 | 1.0 | 2.0 | 3.0 |
index2 | 4.0 | 5.0 | 6.0 | 7.0 |
index3 | 8.0 | 9.0 | 10.0 | NaN |
index4 | 12.0 | 13.0 | 14.0 | 15.0 |
index5 | 16.0 | 17.0 | 18.0 | 19.0 |
frame3.isnull()
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | False | False | False | False |
index2 | False | False | False | False |
index3 | False | False | False | True |
index4 | False | False | False | False |
index5 | False | False | False | False |
# any() 作用:返回是否至少一个元素为真 # 直接求any(),得到的每一列求any()计算的结果 frame3.isnull().any()
column1 False column2 False column3 False column4 True dtype: bool
判断有空值的行
# 方法一:设置any的axis参数 frame3.isnull().any(axis = 1)
index1 False index2 False index3 True index4 False index5 False dtype: bool
# 方法二:先转置再any frame3.isnull().T.any()
index1 False index2 False index3 True index4 False index5 False dtype: bool
应用:取非空值的行
frame3[frame3.isnull().any(axis = 1)==False]
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | 0.0 | 1.0 | 2.0 | 3.0 |
index2 | 4.0 | 5.0 | 6.0 | 7.0 |
index4 | 12.0 | 13.0 | 14.0 | 15.0 |
index5 | 16.0 | 17.0 | 18.0 | 19.0 |
应用:取有空值的行
frame3[frame3.isnull().any(axis = 1)==True]
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index3 | 8.0 | 9.0 | 10.0 | NaN |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
对Python中DataFrame选择某列值为XX的行实例详解
今天小编就为大家分享一篇对Python中DataFrame选择某列值为XX的行实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-01-01python实现图像检索的三种(直方图/OpenCV/哈希法)
这篇文章主要介绍了python实现图像检索的三种(直方图/OpenCV/哈希法),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08举例讲解Linux系统下Python调用系统Shell的方法
这篇文章主要介绍了举例讲解Linux系统下Python调用系统Shell的方法,包括用Python和shell读取文件某一行的实例,需要的朋友可以参考下2015-11-11Python报错:OSError: [Errno 22] Invalid argument解决方案及应用实例
最近跑别人的项目遇到一个这样的问题一开始以为是没有用管理员的权限运行,导致创建不了日志文件后来发现是和windows的命名规则冲突了,这篇文章主要给大家介绍了关于Python报错:OSError: [Errno 22] Invalid argument的解决方案及应用实例,需要的朋友可以参考下2024-07-07
最新评论