我们可以使用如下代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Yes': [50, 21, None, 70, 21], 'No': [131, 2, 98, 1, 3]})
missing_values_count = df.isnull().sum()
print(missing_values_count)
输出为
Yes 1
No 0
dtype: int64
但是如果想自定义输出结果,我们这样写
df = pd.DataFrame({'Yes': [50, 21, None, 70, 21], 'No': [131, 2, 98, 1, 3]})
cols_with_missing = [col for col in df.columns if df[col].isnull().any()]
print(cols_with_missing)
for i in cols_with_missing:
print(i + ': " + str(df[i].isnull().sum()))
第一个打印的是含空值列的列表
第二个打印的是每个空的列含空值个数的统计
此例中结果为
['Yes']
Yes: 1