我想找出我的数据的每一列中NaN的数量。


当前回答

请使用以下方法计算特定的列数

dataframe.columnName.isnull().sum()

其他回答

假设您希望获得一个名为reviews的数据框架中称为price的列(系列)中缺失值(NaN)的数量

#import the dataframe
import pandas as pd

reviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)

用n_missing_prices作为变量来获取缺失的值,简单地做

n_missing_prices = sum(reviews.price.isnull())
print(n_missing_prices)

Sum是这里的关键方法,在我意识到Sum是在这种情况下使用的正确方法之前,我试图使用count

df1.isnull().sum()

这样就可以了。

如果你正在使用Jupyter笔记本,如何....

 %%timeit
 df.isnull().any().any()

or

 %timeit 
 df.isnull().values.sum()

或者,数据中是否存在nan,如果有,在哪里?

 df.isnull().any()

你可以从非nan值的计数中减去总长度:

count_nan = len(df) - df.count()

你应该根据你的数据计算时间。与isnull解相比,小级数的速度提高了3倍。

df.isnull().sum() 
      //type: <class 'pandas.core.series.Series'>

or

df.column_name.isnull().sum()
     //type: <type 'numpy.int64'>