Pandas选择所有不带NaN的列

2024-04-19 09:22:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个200列的DF。大多数都是NaN的。我想选择所有没有NaN的列,或者至少选择最小NaN的列。我尝试过使用阈值或notnull()删除所有列,但没有成功。任何想法。

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  

Tags: truedf阈值nancol2col3col1axis
3条回答

可以使用非NaN列创建

df = df[df.columns[~df.isnull().all()]]

或者

null_cols = df.columns[df.isnull().all()]
df.drop(null_cols, axis = 1, inplace = True)

如果希望基于特定百分比的nan删除列,则将数据超过90%的列称为空

cols_to_delete = df.columns[df.isnull().sum()/len(df) > .90]
df.drop(cols_to_delete, axis = 1, inplace = True)

你应该试试df_notnull = df.dropna(how='all') 这将只得到非空行。

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html

我想你不可能得到所有没有任何NaN的列。如果是这样的话,首先可以使用~col.isnull.any()获得没有任何NaN的列的名称,然后使用该列。

我可以用下面的代码思考:

import pandas as pd

df = pd.DataFrame({
    'col1': [23, 54, pd.np.nan, 87],
    'col2': [45, 39, 45, 32],
    'col3': [pd.np.nan, pd.np.nan, 76, pd.np.nan,]
})

# This function will check if there is a null value in the column
def has_nan(col, threshold=0):
    return col.isnull().sum() > threshold

# Then you apply the "complement" of function to get the column with
# no NaN.

df.loc[:, ~df.apply(has_nan)]

# ... or pass the threshold as parameter, if needed
df.loc[:, ~df.apply(has_nan, args=(2,))]

相关问题 更多 >