Pandas 更好的方法来获取除一列之外所有列都为空的行 [英] Pandas better way to get rows that has all columns null except one

查看:66
本文介绍了Pandas 更好的方法来获取除一列之外所有列都为空的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 列的数据框,其中第一列是唯一标识符,第二列是它们所属的组,该列的其余部分解释了每个用户的特征.

I have a data frame that has 100 columns where the first column is a unique identifier and the second column is the group that they belong to and rest of the column explains each user's characteristics.

user_id, group_id, a, b, c,....,az
0001, 1, 23, pro, 5.5, ......, 234
0002, 1, 32, che, 3.3, ......, 342
0003, 2, NaN, NaN, NaN,......., NaN

我想对除第 1 列和第 2 列(user_idgroup_id)以外的所有列都具有 NaN(s) 的所有记录进行子集化>)

I want to subset all the records that have NaN(s) all the columns except for column 1 and column 2 (user_id, group_id)

一种方法是:df[df['a'].notnull() &(df['b'].notnull()) &(df['c'].notnull())] 但是像这样写 98 列似乎是低效的.有没有更好的办法?

One way to do is: df[df['a'].notnull() & (df['b'].notnull()) & (df['c'].notnull())] but writing for 98 columns like this seems inneficient. Is there a betterway?

推荐答案

In [197]: df[df.iloc[:, 2:].notna().all(1)]
Out[197]:
   user_id  group_id     a    b    c     az
0        1         1  23.0  pro  5.5  234.0
1        2         1  32.0  che  3.3  342.0

如果 user_idgroup_id 总是 设置并且永远不能是 NaN:

if user_id and group_id are always set and can never be NaN's:

In [205]: df[df.notna().sum(1).gt(2)]
Out[205]:
   user_id  group_id     a    b    c     az
0        1         1  23.0  pro  5.5  234.0
1        2         1  32.0  che  3.3  342.0

PS 旧版 Pandas 没有 DataFrame.notna() 方法 - 在这种情况下使用 DataFrame..notnull() 方法

PS older Pandas versions don't have DataFrame.notna() method - in this case use DataFrame..notnull() method

更新:

选择除前两列以外的所有列都具有所有 NAN 值的行:

to select rows where all columns except first two have all NAN values:

In [215]: df[df.iloc[:, 2:].isnull().all(1)]
Out[215]:
   user_id  group_id   a    b   c  az
2        3         2 NaN  NaN NaN NaN

这篇关于Pandas 更好的方法来获取除一列之外所有列都为空的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆