程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

标签  

暂无标签

日期归档  

Python-pandas.DataFrame-找出有空值的行

发布于2019-08-07 10:28     阅读(2511)     评论(0)     点赞(3)     收藏(0)


0.摘要

pandas中DataFrame类型中,找出所有有空值的行,可以使用.isnull()方法和.any()方法。

 

1.找出含有空值的行

方法:DataFrame[DataFrame.isnull().T.any()]

其中,isnull()能够判断数据中元素是否为空值;T为转置;any()判断该行是否有空值。

  1. import pandas as pd
  2. import numpy as np
  3. n = np.arange(20, dtype=float).reshape(5,4)
  4. n[2,3] = np.nan
  5. index = ['index1', 'index2', 'index3', 'index4', 'index5']
  6. columns = ['column1', 'column2', 'column3', 'column4']
  7. frame3 = pd.DataFrame(data=n, index=index, columns=columns)
  8. print(frame3[frame3.isnull().T.any()])

程序成功找到了第三行为有空值的行。

 

2.为什么加转置

在代码中,isnull()的结果需要求转置之后,才能进行any()操作,这是为什么呢?

下面对比一下isnull转置和非转置的情况:

  1. print(frame3.isnull().any())
  2. print("========================")
  3. print(frame3.isnull().T.any())

可见:

非转置:frame3.isnull().any(),得到的每一列求any()计算的结果,输出为列的Series。

转置:frame3.isnull().T.any(),得到的每一行求any()计算的结果,输出为行的Series。



所属网站分类: 技术文章 > 博客

作者:风卷尘沙起

链接:https://www.pythonheidong.com/blog/article/9847/de362bccc5e598a2b5c1/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)