简体   繁体   English

至少有 1 个 Null 值的列名列表以及与每列 pandas 对应的 Null 值总数

[英]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.

相关问题 替换与另一列 pandas 中的特定值相对应的列中的空值 - Replace null values in a column corresponding to specific value in another column pandas 用列表填充 Pandas 列的 Null 值 - Filling Null Values of a Pandas Column with a List 计算至少有一个非 null 响应的列值的数量(列的唯一值的数量) - Count the number of column values (number of unique values of column) that have at least one non null response 选择行值不为空的列名 pandas 数据框 - Select column names where row values are not null pandas dataframe 列出每行中数据框为 NULL/Empty 的列名 - List column names that are NULL/Empty for a Dataframe in each row 将列表的 Pandas 列替换为相应的 dict 值 - Replace a Pandas column of list with corresponding dict values Pyspark - 计算每个数据框列中空值的数量 - Pyspark - Calculate number of null values in each dataframe column 使用 Python Pandas 如何将一列列表值(id 编号)到一列列表值(对应于字典列表中的名称) - Using Python Pandas how to map a column of list values (of id numbers) to a new column of list values (corresponding to names from dictionary list) Pandas:在列的每一行中查找最大值,并在另一列中标识相应的值 - Pandas: Find max value in each row of a column and identify corresponding values in another column Pandas 将行值组合到列表中,用于另一列的每个非空值 - Pandas combine row values into list for every non-null value of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM