在Python Pandas中,检查DataFrame是否有一个(或多个)NaN值的最佳方法是什么?
我知道函数pd。isnan,但这将返回每个元素的布尔值的DataFrame。这篇文章也没有完全回答我的问题。
在Python Pandas中,检查DataFrame是否有一个(或多个)NaN值的最佳方法是什么?
我知道函数pd。isnan,但这将返回每个元素的布尔值的DataFrame。这篇文章也没有完全回答我的问题。
当前回答
条形表示缺少的值
import missingno
missingno.bar(df)# will give you exact no of values and values missing
其他回答
我一直在使用以下和类型转换为字符串,并检查nan值
(str(df.at[index, 'column']) == 'nan')
这允许我检查一个系列中的特定值,而不只是返回如果它包含在系列中的某个地方。
或者你可以在DF上使用.info(),例如:
df.info(null_counts=True)返回列中非_null的行数,例如:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3276314 entries, 0 to 3276313
Data columns (total 10 columns):
n_matches 3276314 non-null int64
avg_pic_distance 3276314 non-null float64
df.apply(axis=0, func=lambda x : any(pd.isnull(x)))
将检查每一列是否包含Nan。
如果你需要知道有多少行有“一个或多个nan”:
df.isnull().T.any().T.sum()
或者如果你需要取出这些行并检查它们:
nan_rows = df[df.isnull().T.any()]
这将只包括至少有一个null/na值的列。
df.isnull().sum()[df.isnull().sum()>0]