在python中的pandas中的DataFrame的dropna中的thresh

AAA*_*AAA 6 python pandas

df1 = pd.DataFrame(np.arange(15).reshape(5,3))
df1.iloc[:4,1] = np.nan
df1.iloc[:2,2] = np.nan
df1.dropna(thresh=1 ,axis=1)
Run Code Online (Sandbox Code Playgroud)

似乎没有删除任何nan值.

    0     1     2
0   0   NaN   NaN
1   3   NaN   NaN
2   6   NaN   8.0
3   9   NaN  11.0
4  12  13.0  14.0
Run Code Online (Sandbox Code Playgroud)

如果我跑

df1.dropna(thresh=2,axis=1)
Run Code Online (Sandbox Code Playgroud)

为什么它给出以下?

    0     2
0   0   NaN
1   3   NaN
2   6   8.0
3   9  11.0
4  12  14.0
Run Code Online (Sandbox Code Playgroud)

我只是不明白th​​resh在这做什么.如果列具有多个nan值,是否应删除该列?

DYZ*_*DYZ 11

thresh=N要求列至少具有N非NaN才能生存.在第一个例子中,两列都至少有一个非NaN,因此两者都存活下来.在第二个示例中,只有最后一列至少有两个非NaN,因此它仍然存在,但前一列被删除.

尝试设置thresh为4以更好地了解正在发生的事情.

  • 谢谢.现在我明白了.我认为纳米值的阈值控制数.我错了.阈值是指非纳米值. (2认同)