我想找出我的数据的每一列中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']))
其他回答
如果你正在使用Jupyter笔记本,如何....
%%timeit
df.isnull().any().any()
or
%timeit
df.isnull().values.sum()
或者,数据中是否存在nan,如果有,在哪里?
df.isnull().any()
import pandas as pd
import numpy as np
# example DataFrame
df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan]})
# count the NaNs in a column
num_nan_a = df.loc[ (pd.isna(df['a'])) , 'a' ].shape[0]
num_nan_b = df.loc[ (pd.isna(df['b'])) , 'b' ].shape[0]
# summarize the num_nan_b
print(df)
print(' ')
print(f"There are {num_nan_a} NaNs in column a")
print(f"There are {num_nan_b} NaNs in column b")
给出输出:
a b
0 1.0 NaN
1 2.0 1.0
2 NaN NaN
There are 1 NaNs in column a
There are 2 NaNs in column b
如果只是在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']))
可以使用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)
希望这能有所帮助,
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan],'c':[np.nan,2,np.nan], 'd':[np.nan,np.nan,np.nan]})
df.isnull().sum()/len(df) * 100
Thres = 40
(df.isnull().sum()/len(df) * 100 ) < Thres