python找出值为nan,Python Pandas查找所有值均为NaN的所有行

这篇博客介绍了如何在Pandas DataFrame中快速找出所有列都包含NaN值的行。通过使用`df.isnull().all(1)`,可以获取一个布尔系列,其中True表示该行所有列都是NaN。然后,利用这个布尔系列作为索引,可以提取出满足条件的行。代码示例中展示了如何创建一个含有随机NaN值的数据框,以及如何找到并显示所有列都为NaN的行。
摘要由CSDN通过智能技术生成

So I have a dataframe with 5 columns. I would like to pull the indices where all of the columns are NaN. I was using this code:

nan = pd.isnull(df.all)

but that is just returning false because it is logically saying no not all values in the dataframe are null. There are thousands of entries so I would prefer to not have to loop through and check each entry. Thanks!

解决方案

It should just be:

df.isnull().all(1)

The index can be accessed like:

df.index[df.isnull().all(1)]

Demonstration

np.random.seed([3,1415])

df = pd.DataFrame(np.random.choice((1, np.nan), (10, 2)))

df

0iX5A.png

idx = df.index[df.isnull().all(1)]

nans = df.ix[idx]

nans

0R6tc.png

Timing

code

np.random.seed([3,1415])

df = pd.DataFrame(np.random.choice((1, np.nan), (10000, 5)))

qD0tb.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值