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


当前回答

df1.isnull().sum()

这样就可以了。

其他回答

下面的代码将按降序打印所有Nan列。

df.isnull().sum().sort_values(ascending = False)

or

下面将按降序打印前15个Nan列。

df.isnull().sum().sort_values(ascending = False).head(15)

可以使用df.iteritems()对数据帧进行循环。在for循环中设置一个条件来计算每列的NaN值百分比,并删除那些包含NaN值超过设置阈值的值:

for col, val in df.iteritems():
    if (df[col].isnull().sum() / len(val) * 100) > 30:
        df.drop(columns=col, inplace=True)

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

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

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

下面是按列计数Null值的代码:

df.isna().sum()

如果只是在pandas列中计算nan值,这里是一个快速的方法

import pandas as pd
## df1 as an example data frame 
## col1 name of column for which you want to calculate the nan values
sum(pd.isnull(df1['col1']))