[英]List of Column names having at least 1 Null value and total number of null values corresponding to each column pandas
Hi I have a code which prints column names along with null values in columns:嗨,我有一个代码,它打印列名以及列中的空值:
A B C D
1 1 4 NAN
2 2 5 NAN
3 NAN 6 NAN
My Code我的代码
[IN]res = list(df.isnull().sum().items())
[IN]print(res)
Current Output电流输出
[('A', 0), ('B', 1), ('C', 0), ('D', 3)]
Expected output:预期输出:
[('B', 1), ('D', 3)]
So basically I wish to remove columns where there are 0 null values and return only columns with at least 1 null value.所以基本上我希望删除有 0 个空值的列,并只返回至少有 1 个空值的列。
First idea is use boolean indexing
:第一个想法是使用
boolean indexing
:
s = df.isnull().sum()
res = list(s[s > 0].items())
print (res)
[('B', 1), ('D', 3)]
Or filter using callable
:或使用
callable
过滤:
res = list(df.isnull().sum()[lambda x: x > 0].items())
Or filter in list comprehension:或者在列表理解中过滤:
res = [(k, v) for k, v in df.isnull().sum().items() if v > 0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.