所以,我有一个像这样的df
,
ID,A,B,C,D,E,F,G
1,123,30,3G,1,123,30,3G
2,456,40,4G,NaN,NaN,NaN,4G
3,789,35,5G,NaN,NaN,NaN,NaN
我还有一个列表,其中包含 df header 列表的子集,如下所示,
header_list = ["D","E","F","G"]
现在我想从 df
获取 header_list 中所有列名称包含空值的记录。
预期输出:
ID,A,B,C,D,E,F,G
3,789,35,5G,NaN,NaN,NaN,NaN
我试过了,
new_df = df[df[header_list].isnull()]
但这会抛出错误,ValueError:条件需要 bool 数组,而不是 float64
我知道我可以做这样的事情
new_df = df[(df['D'].isnull()) & (df['E'].isnull()) & (df['F'].isnull()) & (df['G'].isnull())]
但我不想像这样硬编码。那么有更好的方法吗?
最佳答案
您可以使用以下方式过滤此内容:
df[df[header_list]<b>.isnull().all(axis=1)</b>]
因此,我们检查一行是否包含 .all()
值为 .isnull()
的值。
对于给定的示例输入,这给出了预期的输出:
>>> df[df[header_list].isnull().all(axis=1)]
A B C D E F G
3 789 35 5G NaN NaN NaN NaN
.all(axis=1)
[pandas-doc]因此,如果该行的所有列都为 True
,则该行返回 True
,否则返回 False
。因此,对于给定的示例输入,我们得到:
>>> df[header_list]
D E F G
1 1.0 123.0 30.0 3G
2 NaN NaN NaN 4G
3 NaN NaN NaN NaN
>>> df[header_list].isnull()
D E F G
1 False False False False
2 True True True False
3 True True True True
>>> df[header_list].isnull().all(axis=1)
1 False
2 False
3 True
dtype: bool
关于python - 数据框过滤列列表中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240053/