pandas 选择没有NaN的所有列 [英] Pandas select all columns without NaN
本文介绍了 pandas 选择没有NaN的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个200列的DF.他们大多数是NaN的.我想选择没有NaN或至少具有最小NaN的所有列.我试图用阈值或notnull()删除所有内容,但没有成功.任何想法.
I have a DF with 200 columns. Most of them are with NaN's. I would like to select all columns with no NaN's or at least with the minimum NaN's. I've tried to drop all with a threshold or with notnull() but without success. Any ideas.
df.dropna(thresh=2, inplace=True)
df_notnull = df[df.notnull()]
DF例如:
col1 col2 col3
23 45 NaN
54 39 NaN
NaN 45 76
87 32 NaN
输出应如下所示:
df.dropna(axis=1, thresh=2)
col1 col2
23 45
54 39
NaN 45
87 32
推荐答案
您可以使用非NaN列来创建
You can create with non-NaN columns using
df = df[df.columns[~df.isnull().all()]]
或
null_cols = df.columns[df.isnull().all()]
df.drop(null_cols, axis = 1, inplace = True)
如果您希望基于一定百分比的NaN删除列,则将数据超过90%的列设置为空
If you wish to remove columns based on a certain percentage of NaNs, say columns with more than 90% data as null
cols_to_delete = df.columns[df.isnull().sum()/len(df) > .90]
df.drop(cols_to_delete, axis = 1, inplace = True)
这篇关于 pandas 选择没有NaN的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文