![](/img/trans.png)
[英]Replace null values in a column corresponding to specific value in another column pandas
[英]List of Column names having at least 1 Null value and total number of null values corresponding to each column pandas
嗨,我有一個代碼,它打印列名以及列中的空值:
A B C D
1 1 4 NAN
2 2 5 NAN
3 NAN 6 NAN
我的代碼
[IN]res = list(df.isnull().sum().items())
[IN]print(res)
電流輸出
[('A', 0), ('B', 1), ('C', 0), ('D', 3)]
預期輸出:
[('B', 1), ('D', 3)]
所以基本上我希望刪除有 0 個空值的列,並只返回至少有 1 個空值的列。
第一個想法是使用boolean indexing
:
s = df.isnull().sum()
res = list(s[s > 0].items())
print (res)
[('B', 1), ('D', 3)]
或使用callable
過濾:
res = list(df.isnull().sum()[lambda x: x > 0].items())
或者在列表理解中過濾:
res = [(k, v) for k, v in df.isnull().sum().items() if v > 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.