我想找出我的数据的每一列中NaN的数量。
当前回答
如果只是在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']))
其他回答
请使用以下方法计算特定的列数
dataframe.columnName.isnull().sum()
你可以试试:
In [1]: s = pd.DataFrame('a'=[1,2,5, np.nan, np.nan,3],'b'=[1,3, np.nan, np.nan,3,np.nan])
In [4]: s.isna().sum()
Out[4]: out = {'a'=2, 'b'=3} # the number of NaN values for each column
如果需要nan的总和:
In [5]: s.isna().sum().sum()
Out[6]: out = 5 #the inline sum of Out[4]
如果只是在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']))
下面是按列计数Null值的代码:
df.isna().sum()
我使用这个循环来计算每一列的缺失值:
# check missing values
import numpy as np, pandas as pd
for col in df:
print(col +': '+ np.str(df[col].isna().sum()))